Skip to content

Commit 591c586

Browse files
committed
Allow auto-formating battery files.
1 parent 26115bc commit 591c586

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/config.mts

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ export default class Config {
3030
return outputDirectoryPath;
3131
}
3232

33+
getBatteryDirectoryPaths(): string[] {
34+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
35+
const ciProjectPath = `${__dirname}/../ci/LDKSwift`;
36+
const xcodeProjectPath = `${__dirname}/../xcode/LDKFramework`;
37+
return [
38+
`${ciProjectPath}/Sources/LDKSwift/batteries/`,
39+
`${ciProjectPath}/Tests/LDKSwiftTests/`,
40+
`${xcodeProjectPath}/DirectlyLinkedBindingsApp/`,
41+
`${xcodeProjectPath}/DirectlyLinkedBindingsAppTests/`,
42+
];
43+
}
44+
3345
getSwiftFormatterBinaryPath(): string | null {
3446
let swiftformatPath = process.env['SWIFT_FORMAT_PATH'];
3547
if (!swiftformatPath) {

src/generation/index.mts

+13-5
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,32 @@ export default class Generator {
154154
}
155155
}
156156

157-
async runFormatter() {
157+
async runFormatter(formatBatteryFiles = false) {
158158
const configFilePath = url.fileURLToPath(new URL('.', import.meta.url)) + '../../.swift-format';
159159
const outputDirectory = this.parser.config.getOutputBaseDirectoryPath();
160160
const swiftFormatterBinary = this.parser.config.getSwiftFormatterBinaryPath();
161161
if (!swiftFormatterBinary) {
162162
return;
163163
}
164164

165+
const paths = [outputDirectory];
166+
if (formatBatteryFiles) {
167+
paths.push(...this.parser.config.getBatteryDirectoryPaths());
168+
}
169+
165170
try {
166171
const command = `${swiftFormatterBinary} --configuration ${configFilePath} --recursive --in-place ./ `;
167172
console.log(command);
168-
child_process.execSync(command, {
169-
cwd: outputDirectory
170-
}).toString('utf-8');
173+
for (const currentDirectory of paths) {
174+
console.log(currentDirectory);
175+
child_process.execSync(command, {
176+
cwd: currentDirectory
177+
}).toString('utf-8');
178+
}
171179
} catch (e) {
172180
const errorOutput = e.stderr.toString('utf-8').trim();
173181
console.error('Failed to format Swift output:', errorOutput);
174-
throw new Error(errorOutput)
182+
throw new Error(errorOutput);
175183
}
176184

177185
// after all is generated and formatted, we generate the version file

src/index.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Generator from './generation/index.mjs';
1818

1919
await generator.generateTypes();
2020
await generator.generateFunctions();
21-
await generator.runFormatter();
21+
await generator.runFormatter(true);
2222
})();
2323

2424
interface NullTest {

0 commit comments

Comments
 (0)