|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright |
| 6 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 7 | +# the Apache License, Version 2.0 (the "License"); you may |
| 8 | +# not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +require 'fileutils' |
| 19 | +require_relative '../elasticsearch/lib/elasticsearch/version' |
| 20 | + |
| 21 | +namespace :unified_release do |
| 22 | + desc 'Build gem snapshot' |
| 23 | + task :assemble_snapshot, [:version_qualifier, :output_dir] do |_, args| |
| 24 | + qualifier = if args[:version_qualifier].nil? || args[:version_qualifier].empty? |
| 25 | + @zip_filename = "elasticsearch-ruby-#{Elasticsearch::VERSION}-SNAPSHOT" |
| 26 | + Time.now.strftime('%Y%m%d%H%M%S') |
| 27 | + else |
| 28 | + @zip_filename = "elasticsearch-ruby-#{Elasticsearch::VERSION}-#{args[:version_qualifier]}-SNAPSHOT" |
| 29 | + args[:version_qualifier] |
| 30 | + end |
| 31 | + @version = "#{Elasticsearch::VERSION}.#{qualifier}-SNAPSHOT" |
| 32 | + |
| 33 | + Rake::Task['update_version'].invoke(Elasticsearch::VERSION, @version) unless @version == Elasticsearch::VERSION |
| 34 | + build_gems(args[:output_dir]) |
| 35 | + zip_files(args[:output_dir]) |
| 36 | + end |
| 37 | + |
| 38 | + desc 'Build gem release' |
| 39 | + task :assemble, [:version_qualifier, :output_dir] do |_, args| |
| 40 | + version = [Elasticsearch::VERSION] |
| 41 | + version << args[:version_qualifier] unless args[:version_qualifier].nil? || args[:version_qualifier].empty? |
| 42 | + |
| 43 | + @version = version.join('.') |
| 44 | + @zip_filename = "elasticsearch-ruby-#{version.join('-')}" |
| 45 | + Rake::Task['update_version'].invoke(Elasticsearch::VERSION, @version) unless @version == Elasticsearch::VERSION |
| 46 | + |
| 47 | + build_gems(args[:output_dir]) |
| 48 | + zip_files(args[:output_dir]) |
| 49 | + end |
| 50 | + |
| 51 | + def build_gems(output_dir) |
| 52 | + raise ArgumentError, 'You must specify an output dir' unless output_dir |
| 53 | + |
| 54 | + # Create dir if it doesn't exist |
| 55 | + dir = CURRENT_PATH.join(output_dir).to_s |
| 56 | + FileUtils.mkdir_p(dir) unless File.exist?(dir) |
| 57 | + |
| 58 | + RELEASE_TOGETHER.each do |gem| |
| 59 | + puts '-' * 80 |
| 60 | + puts "Building #{gem} v#{@version} to #{output_dir}" |
| 61 | + sh "cd #{CURRENT_PATH.join(gem)} " \ |
| 62 | + "&& gem build --silent -o #{gem}-#{@version}.gem && " \ |
| 63 | + "mv *.gem #{CURRENT_PATH.join(output_dir)}" |
| 64 | + end |
| 65 | + puts '-' * 80 |
| 66 | + end |
| 67 | + |
| 68 | + def zip_files(output_dir) |
| 69 | + sh "cd #{CURRENT_PATH.join(output_dir)} && " \ |
| 70 | + "zip -r #{@zip_filename}.zip * " \ |
| 71 | + end |
| 72 | + |
| 73 | + desc 'Publish gems to Rubygems' |
| 74 | + task :publish do |
| 75 | + setup_credentials |
| 76 | + |
| 77 | + RELEASE_TOGETHER.each do |gem| |
| 78 | + puts '-' * 80 |
| 79 | + puts "Releasing #{gem} v#{Elasticsearch::VERSION}" |
| 80 | + sh "cd #{CURRENT_PATH.join(gem)} && bundle exec rake release" |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + def setup_credentials |
| 85 | + raise ArgumentError, 'You need to set the env value for GITHUB_TOKEN' unless ENV['GITHUB_TOKEN'] |
| 86 | + raise ArgumentError, 'You need to set the env value for RUBYGEMS_API_KEY' unless ENV['RUBYGEMS_API_KEY'] |
| 87 | + |
| 88 | + sh 'git config --global user.email ${GIT_EMAIL} && ' \ |
| 89 | + 'git config --global user.name ${GIT_NAME}' |
| 90 | + |
| 91 | + file_name = File.expand_path('~/.gem/credentials') |
| 92 | + text = <<~CREDENTIALS |
| 93 | + --- |
| 94 | + :github: Bearer #{ENV['GITHUB_TOKEN']} |
| 95 | + :rubygems_api_key: #{ENV['RUBYGEMS_API_KEY']} |
| 96 | + CREDENTIALS |
| 97 | + File.open(file_name, 'w') do |file| |
| 98 | + file.write(text) |
| 99 | + end |
| 100 | + |
| 101 | + FileUtils.chmod 0o600, file_name |
| 102 | + end |
| 103 | +end |
0 commit comments