-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7efefec
commit bb30bdb
Showing
6 changed files
with
188 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'package:path/path.dart' as path; | ||
|
||
const defaultTempDirPrefix = 'swift2objc_temp_'; | ||
const symbolgraphFileSuffix = '.symbols.json'; | ||
|
||
class Command { | ||
final String executable; | ||
final List<String> args; | ||
|
||
Command({ | ||
required this.executable, | ||
required this.args, | ||
}); | ||
} | ||
|
||
/// Used to configure Swift2ObjC wrapper generation. | ||
class Config { | ||
/// The input to generate a wrapper for. | ||
/// See `FilesInputConfig` and `ModuleInputConfig`; | ||
final InputConfig input; | ||
|
||
/// Specify where the wrapper swift file will be output. | ||
final Uri outputFile; | ||
|
||
/// Specify where to output the intermidiate files (i.g the symbolgraph json). | ||
/// If this is null, a teemp directory will be generated in the system temp | ||
/// directory (using `Directory.systemTemp`) and then deleted. | ||
/// Specifying a temp directory would prevent the tool from deleting the | ||
/// intermediate files after generating the wrapper | ||
final Uri? tempDir; | ||
|
||
const Config({ | ||
required this.input, | ||
required this.outputFile, | ||
this.tempDir, | ||
}); | ||
} | ||
|
||
/// Used to specify the inputs in the `config` object. | ||
/// See `FilesInputConfig` and `ModuleInputConfig` for concrete implementation; | ||
sealed class InputConfig { | ||
Command get symbolgraphCommand; | ||
} | ||
|
||
/// Used to generate a objc wrapper for one or more swift files | ||
class FilesInputConfig implements InputConfig { | ||
/// The swift file(s) to generate a wrapper for | ||
final List<Uri> files; | ||
|
||
/// The name of the module files generated by `swiftc in `tempDir` | ||
final String generatedModuleName; | ||
|
||
FilesInputConfig({ | ||
required this.files, | ||
this.generatedModuleName = 'symbolgraph_module', | ||
}); | ||
|
||
@override | ||
Command get symbolgraphCommand => Command( | ||
executable: 'swiftc', | ||
args: [ | ||
...files.map((uri) => path.absolute(uri.path)), | ||
'-emit-module', | ||
'-emit-symbol-graph', | ||
'-emit-symbol-graph-dir', | ||
'.', | ||
'-module-name', | ||
generatedModuleName | ||
], | ||
); | ||
} | ||
|
||
/// Used to generate a objc wrapper for a built-in swift module | ||
/// (e.g, AVFoundation) | ||
class ModuleInputConfig implements InputConfig { | ||
/// The swift module to generate a wrapper for | ||
final String module; | ||
|
||
/// The target to generate code for | ||
/// (e.g `x86_64-apple-ios17.0-simulator`) | ||
final String target; | ||
|
||
/// The sdk to compile against | ||
/// (e.g `/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sd`) | ||
final Uri sdk; | ||
|
||
ModuleInputConfig({ | ||
required this.module, | ||
required this.target, | ||
required this.sdk, | ||
}); | ||
|
||
@override | ||
Command get symbolgraphCommand => Command( | ||
executable: 'swiftc', | ||
args: [ | ||
'symbolgraph-extract', | ||
'-module-name', | ||
module, | ||
'-target', | ||
target, | ||
'-sdk', | ||
path.absolute(sdk.path), | ||
'-output-dir', | ||
'.', | ||
], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:path/path.dart' as path; | ||
|
||
import 'config.dart'; | ||
import 'generator/generator.dart'; | ||
import 'parser/parser.dart'; | ||
import 'transformer/transform.dart'; | ||
|
||
/// Used to generate the wrapper swift file. | ||
Future<void> generateWrapper(Config config) async { | ||
final Directory tempDir; | ||
final bool deleteTempDirWhenDone; | ||
|
||
if (config.tempDir == null) { | ||
tempDir = Directory.systemTemp.createTempSync(defaultTempDirPrefix); | ||
deleteTempDirWhenDone = true; | ||
} else { | ||
tempDir = Directory.fromUri(config.tempDir!); | ||
deleteTempDirWhenDone = false; | ||
} | ||
|
||
final input = config.input; | ||
|
||
await _generateSymbolgraphJson( | ||
input.symbolgraphCommand, | ||
tempDir, | ||
); | ||
|
||
final symbolgraphFileName = switch (input) { | ||
FilesInputConfig() => '${input.generatedModuleName}$symbolgraphFileSuffix', | ||
ModuleInputConfig() => '${input.module}$symbolgraphFileSuffix', | ||
}; | ||
final symbolgraphJsonPath = path.join(tempDir.path, symbolgraphFileName); | ||
|
||
final declarations = parseAst(symbolgraphJsonPath); | ||
final transformedDeclarations = transform(declarations); | ||
final wrapperCode = generate(transformedDeclarations); | ||
|
||
File.fromUri(config.outputFile).writeAsStringSync(wrapperCode); | ||
|
||
if (deleteTempDirWhenDone) { | ||
tempDir.deleteSync(recursive: true); | ||
} | ||
} | ||
|
||
Future<void> _generateSymbolgraphJson( | ||
Command symbolgraphCommand, | ||
Directory workingDirectory, | ||
) async { | ||
final result = await Process.run( | ||
symbolgraphCommand.executable, | ||
symbolgraphCommand.args, | ||
workingDirectory: workingDirectory.path, | ||
); | ||
|
||
if (result.exitCode != 0) { | ||
throw ProcessException( | ||
symbolgraphCommand.executable, | ||
symbolgraphCommand.args, | ||
'Error generating symbol graph \n${result.stdout} \n${result.stderr}', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters