From 57c651d096f0c5decb7cae7080e75465efb233ba Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Thu, 8 Feb 2024 12:16:15 -0500 Subject: [PATCH] Make documentation generation optional and improve objc path specification (#320) --- stone/backends/swift.py | 8 +-- stone/backends/swift_client.py | 25 +++------- .../swift_rsrc/SwiftReconnectionHelpers.jinja | 2 +- stone/backends/swift_types.py | 49 ++++++++++--------- 4 files changed, 35 insertions(+), 49 deletions(-) diff --git a/stone/backends/swift.py b/stone/backends/swift.py index 2bb81183..6aa89f22 100644 --- a/stone/backends/swift.py +++ b/stone/backends/swift.py @@ -269,14 +269,8 @@ def _docf(self, tag, val): else: return val - def _write_output_in_target_folder(self, output, file_name, objc=False, rop=None): + def _write_output_in_target_folder(self, output, file_name): full_path = self.target_folder_path - if objc: - if rop is None: - full_path = full_path.replace('Source/SwiftyDropbox', 'Source/SwiftyDropboxObjC') - else: - full_path = '{}{}'.format(full_path, rop) - if not os.path.exists(full_path): os.mkdir(full_path) full_path = os.path.join(full_path, file_name) diff --git a/stone/backends/swift_client.py b/stone/backends/swift_client.py index 58c914f6..7a2996ed 100644 --- a/stone/backends/swift_client.py +++ b/stone/backends/swift_client.py @@ -87,15 +87,9 @@ help='The dict that maps a style type to a Swift request object name.', ) _cmdline_parser.add_argument( - '-objc', + '--objc', action='store_true', - help='Generate the Objective-C compatibile files as well as the standard Swift files.', -) -_cmdline_parser.add_argument( - '-rop', - '--relative-objc-path', - type=str, - help='Custom output path for Objective-C compatible files relative to the standard Swift files', + help='Generate the Objective-C compatibile files.', ) @@ -146,7 +140,8 @@ def generate(self, api): self._generate_client(api) self._generate_request_boxes(api) - self._generate_reconnection_helpers(api) + if not self.args.objc: + self._generate_reconnection_helpers(api) def _generate_client(self, api): template_globals = {} @@ -160,9 +155,7 @@ def _generate_client(self, api): template.globals = template_globals self._write_output_in_target_folder(template.render(), - 'DBX{}.swift'.format(self.args.module_name), - True, - self.args.relative_objc_path) + 'DBX{}.swift'.format(self.args.module_name)) else: template = self._jinja_template("SwiftClient.jinja") template.globals = template_globals @@ -224,9 +217,7 @@ def _generate_routes(self, namespace): output_from_parsed_template = template.render(namespace=namespace) self._write_output_in_target_folder(output_from_parsed_template, - 'DBX{}Routes.swift'.format(ns_class), - True, - self.args.relative_objc_path) + 'DBX{}Routes.swift'.format(ns_class)) else: template = self._jinja_template("SwiftRoutes.jinja") template.globals = template_globals @@ -267,9 +258,7 @@ def _generate_request_boxes(self, api): file_name = 'DBX{}RequestBox.swift'.format(self.args.class_name) self._write_output_in_target_folder(output, - file_name, - True, - self.args.relative_objc_path) + file_name) else: template = self._jinja_template("SwiftRequestBox.jinja") template.globals = template_globals diff --git a/stone/backends/swift_rsrc/SwiftReconnectionHelpers.jinja b/stone/backends/swift_rsrc/SwiftReconnectionHelpers.jinja index afae1da4..31f0fdb8 100644 --- a/stone/backends/swift_rsrc/SwiftReconnectionHelpers.jinja +++ b/stone/backends/swift_rsrc/SwiftReconnectionHelpers.jinja @@ -4,7 +4,7 @@ import Foundation -enum {{ class_name }} { +enum {{ class_name }}: ReconnectionHelpersShared { static func rebuildRequest(apiRequest: ApiRequest, client: DropboxTransportClientInternal) throws -> {{ return_type }} { let info = try persistedRequestInfo(from: apiRequest) diff --git a/stone/backends/swift_types.py b/stone/backends/swift_types.py index 90f86c97..fb70e306 100644 --- a/stone/backends/swift_types.py +++ b/stone/backends/swift_types.py @@ -52,18 +52,17 @@ '{route} for the route name.'), ) _cmdline_parser.add_argument( - '-objc', + '--objc', action='store_true', - help='Generate the Objective-C compatibile files as well as the standard Swift files.', + help='Generate the Objective-C compatibile files', ) _cmdline_parser.add_argument( - '-rop', - '--relative-objc-path', - type=str, - help='A custom output path for Objective-C compatible files relative to standard Swift files', + '-d', + '--documentation', + action='store_true', + help=('Sets whether documentation is generated.'), ) - class SwiftTypesBackend(SwiftBaseBackend): """ Generates Swift modules to represent the input Stone spec. @@ -132,19 +131,16 @@ class SwiftTypesBackend(SwiftBaseBackend): cmdline_parser = _cmdline_parser def generate(self, api): rsrc_folder = os.path.join(os.path.dirname(__file__), 'swift_rsrc') - self.logger.info('Copying StoneValidators.swift to output folder') - shutil.copy(os.path.join(rsrc_folder, 'StoneValidators.swift'), - self.target_folder_path) - self.logger.info('Copying StoneSerializers.swift to output folder') - shutil.copy(os.path.join(rsrc_folder, 'StoneSerializers.swift'), - self.target_folder_path) - self.logger.info('Copying StoneBase.swift to output folder') - shutil.copy(os.path.join(rsrc_folder, 'StoneBase.swift'), - self.target_folder_path) - - jazzy_cfg_path = os.path.join('../Format', 'jazzy.json') - with open(jazzy_cfg_path, encoding='utf-8') as jazzy_file: - jazzy_cfg = json.load(jazzy_file) + if not self.args.objc: + self.logger.info('Copying StoneValidators.swift to output folder') + shutil.copy(os.path.join(rsrc_folder, 'StoneValidators.swift'), + self.target_folder_path) + self.logger.info('Copying StoneSerializers.swift to output folder') + shutil.copy(os.path.join(rsrc_folder, 'StoneSerializers.swift'), + self.target_folder_path) + self.logger.info('Copying StoneBase.swift to output folder') + shutil.copy(os.path.join(rsrc_folder, 'StoneBase.swift'), + self.target_folder_path) template_loader = jinja2.FileSystemLoader(searchpath=rsrc_folder) template_env = jinja2.Environment(loader=template_loader, @@ -207,15 +203,22 @@ def generate(self, api): objc_output = objc_template.render(namespace=namespace, route_schema=api.route_schema) self._write_output_in_target_folder(objc_output, - 'DBX{}.swift'.format(ns_class), - True, - self.args.relative_objc_path) + 'DBX{}.swift'.format(ns_class)) else: swift_output = swift_template.render(namespace=namespace, route_schema=api.route_schema) self._write_output_in_target_folder(swift_output, '{}.swift'.format(ns_class)) + if self.args.documentation: + self._generate_jazzy_docs(api) + def _generate_jazzy_docs(self, api): + jazzy_cfg_path = os.path.join('../Format', 'jazzy.json') + with open(jazzy_cfg_path, encoding='utf-8') as jazzy_file: + jazzy_cfg = json.load(jazzy_file) + + for namespace in api.namespaces.values(): + ns_class = fmt_class(namespace.name) jazzy_cfg['custom_categories'][1]['children'].append(ns_class) if namespace.routes: