Skip to content

Commit c1f013c

Browse files
committed
ci(primitives): transition to new css primitives
GitHub no longer distributes primitives in JSON format. Also, the names of the values (CSS variables) have changed. Most of the new names correspond 1-to-1 with one of the old names. Some colors have also changed slightly (e.g. `fg-default`), but otherwise remain mostly the same. See https://primer.style/foundations/primitives/migrating
1 parent 4f44a5c commit c1f013c

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

.github/workflows/csstolua.lua

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
local res = {}
2+
3+
local function set(cssvar, v)
4+
local before, after = cssvar:match('^(.+)%-+(.+)$')
5+
if not after then
6+
res[tonumber(cssvar) or cssvar] = v
7+
return
8+
end
9+
10+
after = tonumber(after) or after
11+
local cur = res
12+
for k in before:gmatch('[^%-_]+') do
13+
k = tonumber(k) or k
14+
cur[k] = cur[k] or {}
15+
cur = cur[k]
16+
end
17+
18+
if type(cur[after]) == 'table' then
19+
cur, after = cur[after], 'default'
20+
end
21+
assert(cur[after] == nil or cur[after] == v)
22+
cur[after] = v
23+
end
24+
25+
local function print_recur(value, _ind)
26+
_ind = _ind or 0
27+
28+
if type(value) == 'table' then
29+
io.write('setmt {')
30+
_ind = _ind + 2
31+
32+
for k, v in pairs(value) do
33+
io.write(('\n%s[%q] = '):format(_ind, k))
34+
print_recur(v, _ind)
35+
io.write(',\n')
36+
end
37+
38+
_ind = _ind - 2
39+
io.write(('%s}'):format(_ind))
40+
else
41+
io.write(('%q'):format(value))
42+
end
43+
end
44+
45+
local defs = {}
46+
for ln in io.lines() do
47+
local k, v = ln:match('^%s*%-%-(%w.-)%s*:%s*(.-)%s*;%s*$')
48+
if k then
49+
table.insert(defs, { k, v })
50+
end
51+
end
52+
53+
table.sort(defs, function(a, b)
54+
return a[1] > b[1]
55+
end)
56+
57+
for _, kv in ipairs(defs) do
58+
set(unpack(kv))
59+
end
60+
61+
assert(res.scale == nil)
62+
res.scale = {}
63+
for color, scale in pairs(res.base.color) do
64+
if type(scale) == 'table' then
65+
res.scale[color] = {}
66+
for i, v in pairs(scale) do
67+
res.scale[color][i + 1] = v
68+
end
69+
else
70+
res.scale[color] = scale
71+
end
72+
end
73+
74+
io.write([=[
75+
local mt = {
76+
__index = function(_, k)
77+
error('invalid index: ' .. k)
78+
end,
79+
}
80+
local function setmt(tbl)
81+
return setmetatable(tbl, mt)
82+
end
83+
local M = ]=])
84+
85+
print_recur(res)
86+
io.write('\n')

.github/workflows/update-color-primitives.yml

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ name: Get/Update Primer Color Primitives
22

33
env:
44
_DEST_DIR: "${{ github.workspace }}/lua/github-theme/palette/primitives"
5-
_JSON_DIR: "${{ github.workspace }}/node_modules/@primer/primitives/dist/json/colors"
5+
_SRC_DIR: "${{ github.workspace }}/node_modules/@primer/primitives/dist/internalCss"
66
_LICENSE_GLOB: "${{ github.workspace }}/node_modules/@primer/primitives/[Ll][Ii][Cc][Ee][Nn][Ss][Ee]*"
77
_PRIMITIVES_PKGJSON: "${{ github.workspace }}/node_modules/@primer/primitives/package.json"
8+
_CSSTOLUA: "${{ github.workspace }}/.github/workflows/csstolua.lua"
89

910
on:
1011
workflow_dispatch:
1112
schedule:
12-
# 3x per week (every other day) at 12:40pm Pacific Time
13-
- cron: "40 19 * * 1,3,5"
13+
# once a week, every Monday at 12:40pm Pacific Time
14+
- cron: "40 19 * * 1"
1415

1516
jobs:
16-
get-colors:
17+
install-primitives:
1718
runs-on: ubuntu-latest
1819
permissions:
1920
checks: write
@@ -25,6 +26,9 @@ jobs:
2526

2627
steps:
2728
- uses: actions/checkout@v4
29+
- uses: rhysd/action-setup-vim@v1
30+
with:
31+
neovim: true
2832

2933
- uses: actions/setup-node@v4
3034
with:
@@ -34,21 +38,22 @@ jobs:
3438
- run: npm i @primer/primitives@latest
3539

3640
- run: |
37-
set -u +f
41+
set -eu +f
3842
shopt -s nocaseglob failglob
3943
license="$(<$_LICENSE_GLOB)"
4044
rm -r "$_DEST_DIR" || :
4145
mkdir -p "$_DEST_DIR"
42-
cd "$_JSON_DIR"
46+
cd "$_SRC_DIR"
4347
4448
if jq -e .version "$_PRIMITIVES_PKGJSON"; then
4549
version="M._VERSION = vim.json.decode([=[$(jq -e .version "$_PRIMITIVES_PKGJSON")]=], { luanil = { object = false, array = false } })"
4650
fi
4751
48-
for file in *.json; do
49-
cat >|"${_DEST_DIR}/${file%.json}.lua" <<EOF
52+
for file in *.css; do
53+
values="$(nvim -l "$_CSSTOLUA" < "$file")"
54+
cat >| "${_DEST_DIR}/${file%.css}.lua" <<EOF
5055
-- NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT EDIT BY-HAND.
51-
local M = vim.json.decode([=[$(<"$file")]=], { luanil = { object = false, array = false } })
56+
${values}
5257
${version-}
5358
M._LICENSE = [=[
5459
$license]=]

0 commit comments

Comments
 (0)