Skip to content

Commit 0b5129e

Browse files
authored
Merge pull request #67 from lightningdevkit/2022-07-spm-checksum-release-mutation
Mutate Package.swift checksum on release
2 parents 98b7ace + ed79ff0 commit 0b5129e

File tree

5 files changed

+134
-21
lines changed

5 files changed

+134
-21
lines changed

.github/workflows/release-framework.yml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ permissions:
88
on:
99
workflow_dispatch:
1010
inputs:
11-
tags:
12-
description: 'Test scenario tags'
11+
tag:
12+
description: 'Tag for next release'
1313
required: true
14-
type: boolean
15-
push:
16-
tags:
17-
- "*"
14+
type: string
1815

1916
jobs:
2017
generate-xcframework:
@@ -30,6 +27,10 @@ jobs:
3027
xcode-version: latest
3128
- name: Checkout
3229
uses: actions/checkout@v2
30+
- name: Update Package.swift tag to ${{ github.event.inputs.tag }}
31+
if: ${{ github.event.inputs.tag != null && github.event.inputs.tag != '' }}
32+
run: |
33+
python3 ./src/scripts/update_swift_package_checksum.py --tag ${{ github.event.inputs.tag }}
3334
- name: Install Dependencies
3435
uses: ./.github/actions/install-dependencies
3536
with:
@@ -38,6 +39,14 @@ jobs:
3839
uses: ./.github/actions/generate-xcframework
3940
- name: Create XCFramework artifact
4041
uses: ./.github/actions/upload-xcframework-artifact
42+
- name: Update Package.swift checksum
43+
if: ${{ github.event.inputs.tag != null && github.event.inputs.tag != '' }}
44+
run: |
45+
CHECKSUM=`swift package compute-checksum LightningDevKit.xcframework.zip`
46+
python3 ./src/scripts/update_swift_package_checksum.py --checksum "${CHECKSUM}"
47+
git commit -m "Update Package.swift for ${{ github.event.inputs.tag }} release." ./Package.swift
48+
git tag ${{ github.event.inputs.tag }}
49+
git push origin HEAD:main --tags
4150
release:
4251
name: Publish Release
4352
runs-on: ubuntu-latest
@@ -48,6 +57,8 @@ jobs:
4857
with:
4958
name: LightningDevKit
5059
- name: Create Release
51-
uses: softprops/action-gh-release@v1
60+
uses: ncipollo/release-action@v1
61+
if: ${{ github.event.inputs.tag != null && github.event.inputs.tag != '' }}
5262
with:
53-
files: LightningDevKit.xcframework.zip
63+
tag: ${{ github.event.inputs.tag }}
64+
artifacts: LightningDevKit.xcframework.zip

.github/workflows/swift.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ jobs:
2929
LDK_SWIFT_GENERATOR_OUTPUT_DIRECTORY_PATH: ci/LDKSwift/Sources/LDKSwift/bindings
3030
- name: Copy new headers into bindings
3131
run: |
32-
cd ci
33-
mkdir -p ./LDKSwift/Sources/LDKHeaders/include
34-
cp ldk-c-bindings/lightning-c-bindings/include/*.h ./LDKSwift/Sources/LDKHeaders/include/
35-
cp ldk-c-bindings/ldk-net/ldk_net.h ./LDKSwift/Sources/LDKHeaders/include/
36-
cp ldk-c-bindings/ldk-net/ldk_net.c ./LDKSwift/Sources/LDKHeaders/
32+
python3 ./src/scripts/copy_c_files.py
33+
env:
34+
LDK_C_BINDINGS_BASE: /home/runner/work/ldk-swift/ldk-swift/ci/ldk-c-bindings
35+
H_FILE_OUTPUT_DIRECTORY: /home/runner/work/ldk-swift/ldk-swift/ci/LDKSwift/Sources/LDKHeaders/include
36+
C_FILE_OUTPUT_DIRECTORY: /home/runner/work/ldk-swift/ldk-swift/ci/LDKSwift/Sources/LDKHeaders
3737
- name: Check that the latest auto-generated Swift files are in the repo
3838
run: |
3939
python3 ./ # Generate bindings into local directory

src/scripts/copy_c_files.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,35 @@ def run(config: ScriptConfig):
1010
os.path.join(config.LDK_C_BINDINGS_BASE, 'ldk-net'),
1111
]
1212

13-
header_destination_directory = config.C_FILE_OUTPUT_DIRECTORY
13+
c_file_destination_directory = config.C_FILE_OUTPUT_DIRECTORY
14+
header_destination_directory = config.H_FILE_OUTPUT_DIRECTORY
15+
os.makedirs(c_file_destination_directory, exist_ok=True)
1416
os.makedirs(header_destination_directory, exist_ok=True)
1517

18+
if c_file_destination_directory != header_destination_directory:
19+
print('Copying C files to', c_file_destination_directory)
1620
print('Copying headers to', header_destination_directory)
1721

1822
for current_directory in header_directories:
1923
for current_file in os.listdir(current_directory):
2024

21-
is_relevant_file = current_file.endswith('.h') or current_file.endswith('.c')
25+
is_header_file = current_file.endswith('.h')
26+
is_c_file = current_file.endswith('.c')
27+
28+
is_relevant_file = is_header_file or is_c_file
2229
if not is_relevant_file:
2330
continue
2431

2532
current_path = os.path.join(current_directory, current_file)
2633
if not os.path.isfile(current_path):
2734
continue
2835

36+
current_destination_directory = header_destination_directory
37+
if is_c_file:
38+
current_destination_directory = c_file_destination_directory
39+
2940
print('Copying', current_path)
30-
subprocess.check_call(['cp', current_path, header_destination_directory])
41+
subprocess.check_call(['cp', current_path, current_destination_directory])
3142

3243

3344
if __name__ == '__main__':

src/scripts/script_config.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self):
3333
self.RUST_CONFIGURATION_FLAG: str = ''
3434
self.LIPO_BINARY_OUTPUT_DIRECTORY: str = ''
3535
self.C_FILE_OUTPUT_DIRECTORY: str = ''
36+
self.H_FILE_OUTPUT_DIRECTORY: str = ''
3637
self.PRESERVE_XCARCHIVES: bool = False
3738

3839
@classmethod
@@ -108,10 +109,19 @@ def parse(cls, allow_ldk_argument=True, parse_configuration=False, parse_lipo_ou
108109
# parse build config aspect
109110
platform = os.getenv('PLATFORM_NAME')
110111
llvm_target_triple_suffix = os.getenv('LLVM_TARGET_TRIPLE_SUFFIX')
111-
architectures = os.getenv('ARCHS').split(' ')
112-
ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures)
113-
config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
114-
115-
pass
112+
architecture_input_string = os.getenv('ARCHS')
113+
if platform and architecture_input_string:
114+
architectures = architecture_input_string.split(' ')
115+
ldkBuildConfig = BuildConfig(platform, llvm_target_triple_suffix, architectures)
116+
config.LIBLDK_BUILD_CONFIGURATIONS = [ldkBuildConfig]
117+
118+
output_directory_override = os.getenv('C_FILE_OUTPUT_DIRECTORY')
119+
if output_directory_override:
120+
config.C_FILE_OUTPUT_DIRECTORY = output_directory_override
121+
122+
config.H_FILE_OUTPUT_DIRECTORY = config.C_FILE_OUTPUT_DIRECTORY
123+
header_output_directory_override = os.getenv('H_FILE_OUTPUT_DIRECTORY')
124+
if header_output_directory_override:
125+
config.H_FILE_OUTPUT_DIRECTORY = header_output_directory_override
116126

117127
return config
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import argparse
2+
import json
3+
import os
4+
import re
5+
import sys
6+
7+
8+
def run(new_checksum: str = None, new_tag: str = None):
9+
if new_checksum is None and new_tag is None:
10+
print('At least one of --checksum or --tag arguments must be provided.', file=sys.stderr)
11+
sys.exit(1)
12+
13+
if new_checksum is not None:
14+
if not new_checksum.isalnum():
15+
print('Checksum must be alphanumeric.', file=sys.stderr)
16+
sys.exit(1)
17+
18+
if not new_checksum.islower():
19+
print('Checksum must be lowercase.', file=sys.stderr)
20+
sys.exit(1)
21+
22+
try:
23+
int(new_checksum, 16)
24+
except:
25+
print('Checksum must be hexadecimal.', file=sys.stderr)
26+
sys.exit(1)
27+
28+
if new_tag is not None:
29+
if new_tag.strip() != new_tag:
30+
print('Tag must not contain any whitespace.', file=sys.stderr)
31+
32+
tag_regex = re.compile("^\d+[.]\d+[.]\d+$")
33+
tag_match = tag_regex.match(new_tag)
34+
if tag_match is None:
35+
print('Tag must adhere to x.x.x major/minor/patch format.', file=sys.stderr)
36+
37+
settings = [
38+
{'variable_name': 'checksum', 'value': new_checksum},
39+
{'variable_name': 'tag', 'value': new_tag},
40+
]
41+
42+
package_file_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '../../Package.swift'))
43+
print(package_file_path)
44+
45+
46+
47+
original_package_file = None
48+
try:
49+
with open(package_file_path, 'r') as package_file_handle:
50+
original_package_file = package_file_handle.read()
51+
except:
52+
print('Failed to read Package.swift file.', file=sys.stderr)
53+
sys.exit(1)
54+
55+
package_file = original_package_file
56+
for current_setting in settings:
57+
current_variable_name = current_setting['variable_name']
58+
new_value = current_setting['value']
59+
if new_value is None:
60+
continue
61+
62+
print(f'setting {current_variable_name} (JSON-serialization):')
63+
print(json.dumps(new_value))
64+
65+
regex = re.compile(f'(let[\s]+{current_variable_name}[\s]*=[\s]*)(.*)')
66+
67+
previous_value = regex.search(package_file).group(2)
68+
# new_package_file = checksum_regex.sub(f'\g<1>"{new_checksum}"', package_file)
69+
package_file = package_file.replace(previous_value, f'"{new_value}"')
70+
71+
with open(package_file_path, "w") as f:
72+
f.write(package_file)
73+
74+
75+
76+
if __name__ == '__main__':
77+
parser = argparse.ArgumentParser(description='Process some integers.')
78+
parser.add_argument('--checksum', type=str, help='new checksum of LightningDevKit.xcframework.zip', required=False, default=None)
79+
parser.add_argument('--tag', type=str, help='new release tag', required=False, default=None)
80+
args = parser.parse_args()
81+
run(new_checksum=args.checksum, new_tag=args.tag)

0 commit comments

Comments
 (0)