github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/clients/build.rb (about)

     1  require 'yaml'
     2  require 'open-uri'
     3  require 'http'
     4  require 'fileutils'
     5  require 'openssl'
     6  
     7  require_relative 'utils.rb'
     8  
     9  swaggerUrl = "https://raw.githubusercontent.com/iron-io/functions/master/docs/swagger.yml"
    10  spec = YAML.load(open(swaggerUrl))
    11  version = spec['info']['version']
    12  puts "VERSION: #{version}"
    13  
    14  # Can pass in a particular language to only do that one
    15  only = ARGV[0]
    16  puts "only building: #{only}" if only
    17  
    18  # Keep getting cert errors??  Had to do this to work around it:
    19  ctx = OpenSSL::SSL::SSLContext.new
    20  ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
    21  
    22  def clone(lang)
    23    Dir.chdir 'tmp'
    24    ldir = "functions_#{lang}"
    25    if !Dir.exist? ldir
    26      cmd = "git clone https://github.com/iron-io/#{ldir}"
    27      stream_exec(cmd)
    28    else
    29      Dir.chdir ldir
    30      cmd = "git pull"
    31      stream_exec(cmd)
    32      Dir.chdir '../'
    33    end
    34    Dir.chdir '../'
    35  end
    36  
    37  FileUtils.mkdir_p 'tmp'
    38  languages = nil
    39  if only
    40    languages = [only]
    41  else 
    42    languages = JSON.parse(HTTP.get("https://generator.swagger.io/api/gen/clients", ssl_context: ctx).body)
    43  end
    44  languages.each do |l|
    45    puts l
    46    lshort = l
    47    # lang_options = JSON.parse(HTTP.get("https://generator.swagger.io/api/gen/clients/#{l}", ssl_context: ctx).body)
    48    # p lang_options
    49    # only going to do ruby and go for now
    50    glob_pattern = ["**", "*"]
    51    copy_dir = "."
    52    options = {}
    53    skip_files = []
    54    deploy = []
    55    case l
    56    when 'go'
    57      clone(lshort)
    58      glob_pattern = ['functions', "**", "*.go"]
    59      copy_dir = "."
    60      options['packageName'] = 'functions'
    61      options['packageVersion'] = version
    62    when 'ruby'
    63      clone(l)
    64      fruby = "functions_ruby"
    65      gem_name = "iron_functions"
    66      glob_pattern = ["**", "*.rb"] # just rb files
    67      skip_files = ["#{gem_name}.gemspec"]
    68      deploy = ["gem build #{gem_name}.gemspec", "gem push #{gem_name}-#{version}.gem"]
    69      options['gemName'] = gem_name
    70      options['moduleName'] = "IronFunctions"
    71      options['gemVersion'] = version
    72      options['gemHomepage'] = "https://github.com/iron-io/#{fruby}"
    73      options['gemSummary'] = 'Ruby gem for IronFunctions'
    74      options['gemDescription'] = 'Ruby gem for IronFunctions.'
    75      options['gemAuthorEmail'] = 'travis@iron.io'
    76    when 'javascript'
    77      lshort = 'js'
    78      # copy_dir = "javascript-client/."
    79      clone(lshort)
    80      options['projectName'] = "iron_functions"
    81      deploy << "npm publish"
    82     else
    83      puts "Skipping #{l}"
    84      next
    85    end
    86    p options
    87    if l == 'go'
    88      puts "SKIPPING GO, it's manual for now."
    89      # This is using https://goswagger.io/ instead
    90      # TODO: run this build command instead: this works if run manually
    91      # dep ensure --update && docker run --rm -it  -v $HOME/dev/go:/go -w /go/src/github.com/iron-io/functions_go quay.io/goswagger/swagger generate client -f https://raw.githubusercontent.com/iron-io/functions/master/docs/swagger.yml -A functions
    92    else
    93      gen = JSON.parse(HTTP.post("https://generator.swagger.io/api/gen/clients/#{l}",
    94      json: {
    95        swaggerUrl: swaggerUrl,
    96        options: options,
    97      },
    98      ssl_context: ctx).body)
    99      p gen
   100  
   101      lv = "#{lshort}-#{version}"
   102      zipfile = "tmp/#{lv}.zip"
   103      stream_exec "curl -o #{zipfile} #{gen['link']} -k"
   104      stream_exec "unzip -o #{zipfile} -d tmp/#{lv}"
   105    end
   106  
   107    # delete the skip_files
   108    skip_files.each do |sf|
   109      begin
   110        File.delete("tmp/#{lv}/#{lshort}-client/" + sf)
   111      rescue => ex
   112        puts "Error deleting file: #{ex.backtrace}"
   113      end
   114    end
   115  
   116    # Copy into clone repos
   117    fj = File.join(['tmp', lv, "#{l}-client"] + glob_pattern)
   118    # FileUtils.mkdir_p "tmp/#{l}-copy"
   119    # FileUtils.cp_r(Dir.glob(fj), "tmp/#{l}-copy")
   120    destdir = "tmp/functions_#{lshort}"
   121    puts "Trying cp", "tmp/#{lv}/#{l}-client/#{copy_dir}", destdir
   122    FileUtils.cp_r("tmp/#{lv}/#{l}-client/#{copy_dir}", destdir)
   123    # Write a version file, this ensures there's always a change.
   124    File.open("#{destdir}/VERSION", 'w') { |file| file.write(version) }
   125  
   126    # Commit and push
   127    begin
   128      Dir.chdir("tmp/functions_#{lshort}")
   129      stream_exec "git add ."
   130      stream_exec "git commit -am \"Updated to api version #{version}\""
   131      begin
   132        stream_exec "git tag -a #{version} -m \"Version #{version}\""
   133      rescue => ex 
   134        puts "WARNING: Tag #{version} already exists."
   135      end
   136      stream_exec "git push --follow-tags"
   137      deploy.each do |d|
   138        stream_exec d
   139      end
   140    rescue ExecError => ex
   141      puts "Error: #{ex}"
   142      if ex.last_line.include?("nothing to commit") || ex.last_line.include?("already exists") || ex.last_line.include?("no changes added to commit")
   143         # ignore this
   144         puts "Ignoring error"
   145      else
   146         raise ex
   147      end
   148    end
   149    Dir.chdir("../../")
   150  
   151  end
   152  
   153  # Uncomment the following lines if we start using the Go lib
   154  # Dir.chdir("../")
   155  # stream_exec "glide up"
   156  Dir.chdir("../tests/")
   157  stream_exec "bundle update"