Skip to content

Commit 9934ff0

Browse files
committed
Add build and workflow
1 parent 4737121 commit 9934ff0

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed

.github/workflows/build_asset.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Build Asset
2+
on:
3+
workflow_dispatch:
4+
branches:
5+
6+
jobs:
7+
build:
8+
9+
name: Build
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@master
15+
16+
- name: Setup Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.11'
20+
21+
- name: Update zip
22+
run: |
23+
cd pcm
24+
python build.py
25+
cd build
26+
echo "ZIP_NAME_WITH=$(ls SparkFun-KiCad-Libraries*-with-pcm.zip)" >> $GITHUB_ENV
27+
echo "PCM_NAME_WITH=$(ls SparkFun-KiCad-Libraries*-with-pcm.zip | rev | cut -c 5- | rev)" >> $GITHUB_ENV
28+
echo "ZIP_NAME_WITHOUT=$(ls SparkFun-KiCad-Libraries*-without-pcm.zip)" >> $GITHUB_ENV
29+
echo "PCM_NAME_WITHOUT=$(ls SparkFun-KiCad-Libraries*-without-pcm.zip | rev | cut -c 5- | rev)" >> $GITHUB_ENV
30+
31+
- name: Upload with-pcm build to action - avoid double-zip
32+
uses: actions/upload-artifact@v4
33+
with:
34+
name: ${{ env.PCM_NAME_WITH }}
35+
path: ./pcm/build/${{ env.PCM_NAME_WITH }}
36+
retention-days: 7
37+
38+
- name: Upload without-pcm build to action - avoid double-zip
39+
uses: actions/upload-artifact@v4
40+
with:
41+
name: ${{ env.PCM_NAME_WITHOUT }}
42+
path: ./pcm/build/${{ env.PCM_NAME_WITHOUT }}
43+
retention-days: 7

pcm/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "9.0.0"

pcm/build.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
import os
3+
from os import path
4+
import shutil
5+
import pathlib
6+
import json
7+
import sys
8+
import fnmatch
9+
10+
src_path = path.join(path.dirname(__file__),'..')
11+
version_path = path.join(path.dirname(__file__),'_version.py')
12+
metadata_template = path.join(path.dirname(__file__),'metadata_template.json')
13+
resources_path = path.join(path.dirname(__file__),'resources')
14+
build_path = path.join('build')
15+
16+
# https://stackoverflow.com/a/6257321
17+
def findReplace(directory, find, replace, filePattern):
18+
for path, dirs, files in os.walk(os.path.abspath(directory)):
19+
for filename in fnmatch.filter(files, filePattern):
20+
filepath = os.path.join(path, filename)
21+
with open(filepath) as f:
22+
s = f.read()
23+
s = s.replace(find, replace)
24+
with open(filepath, "w") as f:
25+
f.write(s)
26+
27+
# ======================================================
28+
29+
# Delete build and recreate
30+
try:
31+
shutil.rmtree(build_path)
32+
except FileNotFoundError:
33+
pass
34+
os.mkdir(build_path)
35+
os.mkdir(path.join(build_path,'content'))
36+
os.chdir(build_path)
37+
38+
# Copy symbols, footprints and 3dmodels
39+
shutil.copytree(path.join(src_path,'symbols'), path.join('content'))
40+
shutil.copytree(path.join(src_path,'footprints'), path.join('content'))
41+
shutil.copytree(path.join(src_path,'3dmodels'), path.join('content'))
42+
43+
# Replace every occurrence of "(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/"
44+
# with "(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_sparkfun-kicad-libraries/3d-library.3dshapes/"
45+
findReplace(path.join('content'), '(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/',
46+
'(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_SparkFun-KiCad-Libraries/3d-library.3dshapes/', '*.*')
47+
48+
# This is the with-PCM_ version
49+
# Replace every occurrence of "(symbol "SparkFun-"
50+
# with "(symbol "PCM_SparkFun-"
51+
findReplace(path.join('content'), "(symbol \"SparkFun-",
52+
"(symbol \"PCM_SparkFun-", "*.*")
53+
54+
# Copy icon
55+
shutil.copytree(resources_path, path.join('content','resources'))
56+
57+
# Copy metadata
58+
metadata = path.join('content','metadata.json')
59+
shutil.copy(metadata_template, metadata)
60+
61+
# Load up json script
62+
with open(metadata) as f:
63+
md = json.load(f)
64+
65+
# Get version from _version.py
66+
# https://stackoverflow.com/a/7071358
67+
import re
68+
verstrline = open(version_path, "rt").read()
69+
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
70+
mo = re.search(VSRE, verstrline, re.M)
71+
if mo:
72+
verstr = mo.group(1)
73+
else:
74+
verstr = "9.0.0"
75+
76+
# Update download URL
77+
md['versions'][0].update({
78+
'version': verstr,
79+
'download_url': 'https://github.com/sparkfun/SparkFun-KiCad-Libraries/releases/download/v{0}/SparkFun-KiCad-Libraries-{0}-with-pcm.zip'.format(verstr)
80+
})
81+
82+
# Update metadata.json
83+
with open(metadata, 'w') as of:
84+
json.dump(md, of, indent=2)
85+
86+
# Zip all files
87+
zip_file = 'SparkFun-KiCad-Libraries-{0}-with-pcm.zip'.format(md['versions'][0]['version'])
88+
shutil.make_archive(pathlib.Path(zip_file).stem, 'zip', 'content')
89+
90+
# Rename the content directory so we can upload it as an artifact - and avoid the double-zip
91+
shutil.move('content', 'SparkFun-KiCad-Libraries-{0}-with-pcm'.format(md['versions'][0]['version']))
92+
93+
# ======================================================
94+
95+
# Do it all again for the without-PCM_ version
96+
os.mkdir(path.join('content'))
97+
98+
# Copy symbols, footprints and 3dmodels
99+
shutil.copytree(path.join(src_path,'symbols'), path.join('content'))
100+
shutil.copytree(path.join(src_path,'footprints'), path.join('content'))
101+
shutil.copytree(path.join(src_path,'3dmodels'), path.join('content'))
102+
103+
# Replace every occurrence of "(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/"
104+
# with "(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_sparkfun-kicad-libraries/3d-library.3dshapes/"
105+
findReplace(path.join('content'), '(model "${SPARKFUN_KICAD_LIBRARY}/3dmodels/3d-library.3dshapes/',
106+
'(model "${KICAD9_3RD_PARTY}/3dmodels/com_github_sparkfun_SparkFun-KiCad-Libraries/3d-library.3dshapes/', '*.*')
107+
108+
# Copy icon
109+
shutil.copytree(resources_path, path.join('content','resources'))
110+
111+
# Copy metadata
112+
metadata = path.join('content','metadata.json')
113+
shutil.copy(metadata_template, metadata)
114+
115+
# Load up json script
116+
with open(metadata) as f:
117+
md = json.load(f)
118+
119+
# Get version from _version.py
120+
# https://stackoverflow.com/a/7071358
121+
import re
122+
verstrline = open(version_path, "rt").read()
123+
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
124+
mo = re.search(VSRE, verstrline, re.M)
125+
if mo:
126+
verstr = mo.group(1)
127+
else:
128+
verstr = "9.0.0"
129+
130+
# Update download URL
131+
md['versions'][0].update({
132+
'version': verstr,
133+
'download_url': 'https://github.com/sparkfun/SparkFun-KiCad-Libraries/releases/download/v{0}/SparkFun-KiCad-Libraries-{0}-without-pcm.zip'.format(verstr)
134+
})
135+
136+
# Update metadata.json
137+
with open(metadata, 'w') as of:
138+
json.dump(md, of, indent=2)
139+
140+
# Zip all files
141+
zip_file = 'SparkFun-KiCad-Libraries-{0}-without-pcm.zip'.format(md['versions'][0]['version'])
142+
shutil.make_archive(pathlib.Path(zip_file).stem, 'zip', 'content')
143+
144+
# Rename the content directory so we can upload it as an artifact - and avoid the double-zip
145+
shutil.move('content', 'SparkFun-KiCad-Libraries-{0}-without-pcm'.format(md['versions'][0]['version']))

pcm/metadata_template.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"$schema": "https://go.kicad.org/pcm/schemas/v1",
3+
"name": "SparkFun KiCad Libraries",
4+
"description": "SparkFun's Libraries for KiCad 7 / 8 / 9",
5+
"description_full": "This library contains SparkFun's commonly used parts. It is a cultivated combination of KiCad stock parts, SparkFun-unique footprints, and open-source 3D models.",
6+
"identifier": "com.github.sparkfun.SparkFun-KiCad-Libraries",
7+
"type": "library",
8+
"author": {
9+
"name": "SparkFun",
10+
"contact": {
11+
"github": "https://github.com/sparkfun",
12+
"web": "https://sparkfun.com",
13+
"twitter": "https://twitter.com/sparkfun"
14+
}
15+
},
16+
"maintainer": {
17+
"name": "SparkFun",
18+
"contact": {
19+
"github": "https://github.com/sparkfun",
20+
"web": "https://sparkfun.com",
21+
"twitter": "https://twitter.com/sparkfun"
22+
}
23+
},
24+
"license": "CC-BY-4.0",
25+
"resources": {
26+
"homepage": "https://github.com/sparkfun/SparkFun-KiCad-Libraries"
27+
},
28+
"versions": [
29+
{
30+
"version": "9.0.0",
31+
"status": "stable",
32+
"kicad_version": "7.0"
33+
}
34+
]
35+
}

pcm/resources/icon.png

7.62 KB
Loading

0 commit comments

Comments
 (0)