Skip to content
This repository was archived by the owner on Feb 28, 2020. It is now read-only.

feat: enable iOS sdk generation after invoking model generator #260

Merged
merged 7 commits into from
Jul 20, 2017
22 changes: 18 additions & 4 deletions refresh/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,19 +674,21 @@ module.exports = Generator.extend({
},

generateSDKs: function () {
var shouldGenerateClientWithModel = (!!this.swagger && JSON.stringify(this.swagger['paths']) !== '{}')
var shouldGenerateClient = (!!this.fromSwagger)
var shouldGenerateServer = (this.serverSwaggerFiles.length > 0)
if (!shouldGenerateClient && !shouldGenerateServer) return
if (!shouldGenerateClientWithModel && !shouldGenerateClient && !shouldGenerateServer) return

this.log(chalk.green('Generating SDK(s) from swagger file(s)...'))
var generationTasks = []
if (shouldGenerateClient) generationTasks.push(generateClientAsync.call(this))
if (shouldGenerateClientWithModel) generationTasks.push(generateClientAsync.call(this, this.swagger))
if (shouldGenerateClient) generationTasks.push(generateClientAsync.call(this, this.loadedApi))
if (shouldGenerateServer) generationTasks.push(generateServerAsync.call(this))
return Promise.all(generationTasks)

function generateClientAsync () {
function generateClientAsync (swaggerContent) {
var clientSDKName = `${this.projectName}_iOS_SDK`
return performSDKGenerationAsync(clientSDKName, 'ios_swift', JSON.stringify(this.loadedApi))
return performSDKGenerationAsync(clientSDKName, 'ios_swift', JSON.stringify(swaggerContent))
.then(generatedID => getClientSDKAsync(clientSDKName, generatedID))
.then(() => {
this.itemsToIgnore.push(`/${clientSDKName}*`)
Expand Down Expand Up @@ -1041,6 +1043,18 @@ module.exports = Generator.extend({
var swaggerFilename = this.destinationPath('definitions', `${this.projectName}.yaml`)
this.conflicter.force = true
this.fs.write(swaggerFilename, YAML.safeDump(this.swagger))

// Append items to .gitignore that may have been added through model gen process
var gitignoreFile = this.fs.read(this.destinationPath('.gitignore'))
for (var item in this.itemsToIgnore) {
// Ensure we only add unique values
if (gitignoreFile.indexOf(this.itemsToIgnore[item]) === -1) {
this.fs.append(
this.destinationPath('.gitignore'),
this.itemsToIgnore[item]
)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the above block, I'm trying to see when this might be needed. Can you help me understand the flow that introduces new entries in this.itemsToIgnore after the .gitignore file is written? Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tunniclm Well I first noticed it in the integration tests that multiple references to an iOS SDK were being added to the .gitignore. It can also happen with multiple runs of the yo swiftserver:model command, which is possible right now, but maybe not encouraged, not sure?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks. Makes total sense. It is absolutely fine to run the model generator multiple times.

}
},

Expand Down
11 changes: 11 additions & 0 deletions test/integration/model/prompted_nobuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,16 @@ describe('Prompt and no build integration tests for model generator', function (
}
assert.jsonFileContent('spec.json', expected)
})

it('created a iOS SDK zip file', function () {
assert.file('test_iOS_SDK.zip')
})

it('added reference to generated iOS SDK to .gitignore', function () {
// ensure reference is present and that there is only one reference to iOS SDK
var fileContent = fs.readFileSync('.gitignore').toString('utf-8')
var occurrences = (fileContent.match(/\/test_iOS_SDK\*/g) || []).length
assert.equal(occurrences, 1)
})
})
})