Swagger client generator. (#166)

This commit is contained in:
Travis Reeder
2016-10-14 11:40:20 -07:00
committed by GitHub
parent 6b7886721d
commit fcde29802e
6 changed files with 193 additions and 7 deletions

3
clients/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.zip
tmp/
private.sh

30
clients/README.md Normal file
View File

@@ -0,0 +1,30 @@
`build.rb` will generate all clients for all Swagger supported languages.
## Building and Deploying Clients
### First Time
If this is your first time building the clients, you'll need to do the following:
1) Install this single gem:
```sh
gem install http
```
2) Create a secret gist on Github and get an API token too.
### Every Time
Everytime the API spec is updated, be sure to bump the version number in `swagger.yml`, then run:
```sh
# The GITHUB_TOKEN, GITHUB_USERNAME and GIST_ID requirement is temporary, just used to automate the gist. Once this is public, we don't need the gist anymore.
GITHUB_TOKEN=X GITHUB_USERNAME=treeder GIST_ID=Y ruby build.rb
```
Boom. That's it.
## Troubleshooting
Sometimes this will fail due to github caching or something and versions will be off. Just bump version and retry.

150
clients/build.rb Normal file
View File

@@ -0,0 +1,150 @@
require 'yaml'
require 'open-uri'
require 'http'
require 'fileutils'
require 'openssl'
require_relative '../test/utils.rb'
gist_id = ENV['GIST_ID']
gist_url = "https://api.github.com/gists/#{gist_id}"
puts gist_url
HTTP.auth("Token #{ENV['GITHUB_TOKEN']}")
.patch(gist_url, :json => {
"files"=> {
"swagger.yml" => {
"content" => File.read('../docs/swagger.yml')
}
}
})
swaggerUrl = "https://gist.githubusercontent.com/#{ENV['GITHUB_USERNAME']}/#{gist_id}/raw/"
# swaggerRaw = open(swaggerUrl){|f| f.read}
spec = YAML.load(open(swaggerUrl))
version = spec['info']['version']
puts "VERSION: #{version}"
# Keep getting cert errors?? Had to do this to work around it:
ctx = OpenSSL::SSL::SSLContext.new
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
def clone(lang)
Dir.chdir 'tmp'
ldir = "functions_#{lang}"
if !Dir.exists? ldir
cmd = "git clone https://github.com/iron-io/#{ldir}"
stream_exec(cmd)
else
Dir.chdir ldir
cmd = "git pull"
stream_exec(cmd)
Dir.chdir '../'
end
Dir.chdir '../'
end
FileUtils.mkdir_p 'tmp'
languages = JSON.parse(HTTP.get("https://generator.swagger.io/api/gen/clients", ssl_context: ctx).body)
languages.each do |l|
puts l
lshort = l
# lang_options = JSON.parse(HTTP.get("https://generator.swagger.io/api/gen/clients/#{l}", ssl_context: ctx).body)
# p lang_options
# only going to do ruby and go for now
glob_pattern = ["**", "*"]
copy_dir = "."
options = {}
skip_files = []
deploy = []
case l
when 'go'
clone(lshort)
glob_pattern = ['functions', "**", "*.go"]
copy_dir = "."
options['packageName'] = 'functions'
options['packageVersion'] = version
when 'ruby'
clone(l)
fruby = "functions_ruby"
gem_name = "iron_functions"
glob_pattern = ["**", "*.rb"] # just rb files
skip_files = ["#{gem_name}.gemspec"]
deploy = ["gem build #{gem_name}.gemspec", "gem push #{gem_name}-#{version}.gem"]
options['gemName'] = gem_name
options['moduleName'] = "IronFunctions"
options['gemVersion'] = version
options['gemHomepage'] = "https://github.com/iron-io/#{fruby}"
options['gemSummary'] = 'Ruby gem for IronFunctions'
options['gemDescription'] = 'Ruby gem for IronFunctions.'
options['gemAuthorEmail'] = 'travis@iron.io'
when 'javascript'
lshort = 'js'
# copy_dir = "javascript-client/."
clone(lshort)
options['projectName'] = "iron_functions"
deploy << "npm publish"
else
puts "Skipping #{l}"
next
end
p options
gen = JSON.parse(HTTP.post("https://generator.swagger.io/api/gen/clients/#{l}",
json: {
swaggerUrl: swaggerUrl,
options: options,
},
ssl_context: ctx).body)
p gen
lv = "#{lshort}-#{version}"
zipfile = "tmp/#{lv}.zip"
stream_exec "curl -o #{zipfile} #{gen['link']} -k"
stream_exec "unzip -o #{zipfile} -d tmp/#{lv}"
# delete the skip_files
skip_files.each do |sf|
begin
File.delete("tmp/#{lv}/#{lshort}-client/" + sf)
rescue => ex
puts "Error deleting file: #{ex.backtrace}"
end
end
# Copy into clone repos
fj = File.join(['tmp', lv, "#{l}-client"] + glob_pattern)
# FileUtils.mkdir_p "tmp/#{l}-copy"
# FileUtils.cp_r(Dir.glob(fj), "tmp/#{l}-copy")
destdir = "tmp/functions_#{lshort}"
puts "Trying cp", "tmp/#{lv}/#{l}-client/#{copy_dir}", destdir
FileUtils.cp_r("tmp/#{lv}/#{l}-client/#{copy_dir}", destdir)
# Write a version file, this ensures there's always a change.
File.open("#{destdir}/VERSION", 'w') { |file| file.write(version) }
# Commit and push
begin
Dir.chdir("tmp/functions_#{lshort}")
stream_exec "git add ."
stream_exec "git commit -am \"Updated to api version #{version}\""
stream_exec "git tag -a #{version} -m \"Version #{version}\""
stream_exec "git push --follow-tags"
deploy.each do |d|
stream_exec d
end
rescue ExecError => ex
puts "Error: #{ex}"
if ex.last_line.include?("nothing to commit") || ex.last_line.include?("already exists") || ex.last_line.include?("no changes added to commit")
# ignore this
puts "Ignoring error"
else
raise ex
end
end
Dir.chdir("../../")
end
# Uncomment the following lines if we start using the Go lib
# Dir.chdir("../")
# stream_exec "glide up"
Dir.chdir("../test/")
stream_exec "bundle update"

View File

@@ -6,7 +6,7 @@ swagger: '2.0'
info:
title: IronFunctions
description:
version: "0.0.1"
version: "0.0.5"
# the domain of the service
host: "127.0.0.1:8080"
# array of all schemes that your API supports
@@ -73,7 +73,7 @@ paths:
tags:
- Apps
parameters:
- name: name
- name: app
in: path
description: name of the app.
required: true

7
glide.lock generated
View File

@@ -28,7 +28,7 @@ imports:
- name: github.com/dgrijalva/jwt-go
version: 268038b363c7a8d7306b8e35bf77a1fde4b0c402
- name: github.com/docker/distribution
version: 252cc27ab1272108bfd74cf939d354db77862d93
version: 04c8db562da407630075b2452daee09b894a070a
subpackages:
- context
- digest
@@ -61,7 +61,7 @@ imports:
- name: github.com/fsnotify/fsnotify
version: f12c6236fe7b5cf6bcf30e5935d08cb079d78334
- name: github.com/fsouza/go-dockerclient
version: 4bae70ae976745ea1cd0db139b53f83a7bcb898d
version: 5e508da6ac33603fcc7dc200b670ba295f24d975
- name: github.com/garyburd/redigo
version: 4ed1111375cbeb698249ffe48dd463e9b0a63a7a
subpackages:
@@ -92,6 +92,8 @@ imports:
version: 0e04f5e499b19bf51031c01a00f098f25067d8dc
- name: github.com/go-openapi/validate
version: e6da236c48ce621803fc5d3883d8739aad0ce318
- name: github.com/go-resty/resty
version: 1a3bb60986d90e32c04575111b1ccb8eab24a3e5
- name: github.com/golang/protobuf
version: 2402d76f3d41f928c7902a765dfc872356dd3aad
subpackages:
@@ -202,6 +204,7 @@ imports:
- context/ctxhttp
- idna
- proxy
- publicsuffix
- name: golang.org/x/sys
version: 9eef40adf05b951699605195b829612bd7b69952
subpackages:

View File

@@ -9,11 +9,11 @@ GIT
GEM
remote: http://rubygems.org/
specs:
ethon (0.9.0)
ethon (0.9.1)
ffi (>= 1.3.0)
ffi (1.9.14)
json (1.8.3)
power_assert (0.3.0)
power_assert (0.3.1)
test-unit (3.2.1)
power_assert
typhoeus (1.1.0)
@@ -27,4 +27,4 @@ DEPENDENCIES
worker_ruby (>= 0.3.5)!
BUNDLED WITH
1.12.5
1.13.3