SourceKitten icon indicating copy to clipboard operation
SourceKitten copied to clipboard

Using SKitten to create LinuxMain.swift file

Open felix91gr opened this issue 7 years ago • 7 comments

Would it be possible? I've been trying to make a decent python script to do this, but the lack of semantics makes it fragile: I can't know whether a class inherits from XCTest or know the relationship between two classes in different files. E.g: Foo inherits from Bar, and Bar inherits from XCTest, but in an extension. The combinations are endless, which seriously limits what I can make with just regexes. I'd need to make a compiler to address those issues 😅

Which brings me to SourceKit and therefore, SourceKitten. Could I do something like this:

  • In a directory, get all Swift files and extract the classes that inherit from XCTest from them.
  • Then, for each class, extract the names of all test methods that belong to it.

Just that would suffice. The rest is easy and does not require semantic knowledge (it's just string manipulation from then on).

Could I do this with SourceKitten?

felix91gr avatar Jun 08 '18 07:06 felix91gr

I think you can achieve your goal by creating a Sourcery Template.

ainopara avatar Jun 08 '18 10:06 ainopara

Yes... that's what I guessed. It looks promising, but the problem is that atm Sourcery isn't available for Linux u.u

felix91gr avatar Jun 08 '18 13:06 felix91gr

Ah... That's unlucky. You can still create a tool based on SourceKitten for your goal.

ainopara avatar Jun 08 '18 17:06 ainopara

Where should I start reading? Is there documentation on SKitten's API?

felix91gr avatar Jun 08 '18 19:06 felix91gr

You can try executing sourcekitten structure --file path_to_your_source_file on every file in your test folder and collect json formatted outputs from sourcekitten.

ainopara avatar Jun 08 '18 20:06 ainopara

Thank you! I'm starting to understand what the meaning of that JSON's fields are. I'm gonna see how far I can take this! :)

felix91gr avatar Jun 08 '18 20:06 felix91gr

I gave a talk at App Builders 2016 demonstrating how you can use the sourcekitten CLI and jq to generate LinuxMain.swift, which might be a good starting point for you (slide 28):

  • https://realm.io/news/appbuilders-jp-simard-sourcekit
  • https://speakerdeck.com/jpsim/sourcekit-and-you

You could easily map the ideas from the CLI proof of concept to do so using SourceKittenFramework directly.

jpsim avatar Jun 24 '18 15:06 jpsim