Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cocoa enum conversion script #101

Merged
merged 5 commits into from
Dec 27, 2017

Conversation

DivineDominion
Copy link
Contributor

@DivineDominion DivineDominion commented Dec 27, 2017

I polished the script some more to be usable from the command line. It is located at swift/cocoaconv.rb.

The script supports a 2-step process:

  1. generate NS_ENUM definitions in Objective-C that uses the naming conventions necessary for proper Swift bridging (default)
  2. optionally generate more useful string representations for debugging in Swift (-m swift option)

Example output

typedef NS_ENUM(NSUInteger, MMD6ParserExtension) {
	MMD6ParserExtensionCompatibility = EXT_COMPATIBILITY,
	MMD6ParserExtensionComplete = EXT_COMPLETE,
	MMD6ParserExtensionSnippet = EXT_SNIPPET,
	MMD6ParserExtensionSmart = EXT_SMART,
	MMD6ParserExtensionNotes = EXT_NOTES,
	MMD6ParserExtensionNoLabels = EXT_NO_LABELS,
	MMD6ParserExtensionProcessHtml = EXT_PROCESS_HTML,
	MMD6ParserExtensionNoMetadata = EXT_NO_METADATA,
	MMD6ParserExtensionObfuscate = EXT_OBFUSCATE,
	MMD6ParserExtensionCritic = EXT_CRITIC,
	MMD6ParserExtensionCriticAccept = EXT_CRITIC_ACCEPT,
	MMD6ParserExtensionCriticReject = EXT_CRITIC_REJECT,
	MMD6ParserExtensionRandomFoot = EXT_RANDOM_FOOT,
	MMD6ParserExtensionTransclude = EXT_TRANSCLUDE,
	MMD6ParserExtensionFake = EXT_FAKE,
} NS_SWIFT_NAME(ParserExtension);
extension ParserExtension: CustomStringConvertible {
    public var description: String {
        switch self {
            case .compatibility: return "ParserExtension.compatibility"
            case .complete: return "ParserExtension.complete"
            case .snippet: return "ParserExtension.snippet"
            case .smart: return "ParserExtension.smart"
            case .notes: return "ParserExtension.notes"
            case .noLabels: return "ParserExtension.noLabels"
            case .processHtml: return "ParserExtension.processHtml"
            case .noMetadata: return "ParserExtension.noMetadata"
            case .obfuscate: return "ParserExtension.obfuscate"
            case .critic: return "ParserExtension.critic"
            case .criticAccept: return "ParserExtension.criticAccept"
            case .criticReject: return "ParserExtension.criticReject"
            case .randomFoot: return "ParserExtension.randomFoot"
            case .transclude: return "ParserExtension.transclude"
            case .fake: return "ParserExtension.fake"
        }
    }
}

@DivineDominion
Copy link
Contributor Author

DivineDominion commented Dec 27, 2017

Run the script like this to generate the Objective-C code:

ruby swift/cocoaconv.rb -m nsenum /build-xcode/Debug/include/libMultiMarkdown/libMultiMarkdown.h

Should the NS_ENUM generator also add header information like the following to make the result ready to be dropped into the project?

#import <Foundation/Foundation.h>
#import <libMultiMarkdown/libMultiMarkdown.h>
#import <libMultiMarkdown/token.h>
#import <libMultiMarkdown/d_string.h>

typedef NS_ENUM(NSUInteger, MMD6TokenType) {
// ...

In the long run, I think something like this is useful to generate a Cocoa framework. I wonder if wrapping all this in another project or Swift Package Manager package is necessary to achieve that.

@fletcher
Copy link
Owner

Going on faith that this is correct, since I don't use swift for much beyond tinkering with a few lines of code.

@fletcher fletcher merged commit 9f6a895 into fletcher:develop Dec 27, 2017
@DivineDominion
Copy link
Contributor Author

DivineDominion commented Dec 27, 2017

I can still change the resulting public API etc. so that it matches your vision of the project.

Edit: Oh, noticed the changes are merged already :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants