summaryrefslogtreecommitdiff
path: root/lib/pgxn_utils/no_tasks.rb
blob: 9dc62ad8be0099e3d805eab55e3565fc20baaeb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module PgxnUtils
  module NoTasks

    include PgxnUtils::Constants
	include Grit

	def init_repository(extension_dir)
		repo = Repo.init(extension_dir)
		original_dir = File.expand_path "."

		if repo
			say_status :init, "#{extension_dir}", :green

			FileUtils.chdir extension_dir
			repo.add "."
			if repo.commit_index("initial commit")
				say_status :commit, "initial commit", :green
			else
				say_status :failed, "initial commit", :red
			end
		else
			say_status :error, " initializing #{extension_dir}", :red
		end

		FileUtils.chdir original_dir
	end

	def is_dirty?(extension_dir)
		repo = Repo.init(extension_dir)
		repo.status.map(&:type).uniq != [nil]
	end

    def make_dist_clean(path)
      inside path do
        run 'make distclean', :capture => true
      end
    end

    def ask_for_pgxn_credential
      self.pgxn_username = ENV["PGXN_USER"] || HighLine.ask("Enter your PGXN username: ") { |q| q.validate = /^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$/ }
      self.pgxn_password = ENV["PGXN_PASS"] || HighLine.ask("Enter your PGXN password: ") { |q| q.echo =  '*' }
    end

    def check_response(response)
      case response
      when Net::HTTPUnauthorized then
        say "oops!", :red
        say "It seems that you entered a wrong username or password.", :red
      when Net::HTTPConflict then
        say "conflict!", :yellow
        say "Distribution already exists! Please, check your META.json.", :yellow
      when Net::HTTPSeeOther then
        say "released successfully!", :green
        say "Visit: #{URI.parse(response['Location'])}", :green
      else
        say "Unknown error. (#{response})"
      end
    end

    def prepare_multipart_post_for(filename)
      file_basename = File.basename(filename)
      zip_file = File.open(filename)
      Net::HTTP::Post::Multipart.new(
        UPLOAD_URL.path,
        "archive" => UploadIO.new(zip_file, "application/zip", file_basename),
        "Expect" => ""
      )
    end

    def try_send_file(request, filename)
      begin
        http = Net::HTTP.new(UPLOAD_URL.host, UPLOAD_URL.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        say "Trying to release #{File.basename(filename)} ... "
        http.request(request)
      rescue SocketError
        say "Please, check your connection.", :red
        exit(1)
      end
    end

    def send_file_to_pgxn(filename)
      request = prepare_multipart_post_for(filename)
      ask_for_pgxn_credential

      request.basic_auth pgxn_username, pgxn_password
      response = try_send_file(request, filename)
      check_response(response)
    end

    def resolve_extension_path_and_name(extension_name)
      target = options[:target]
      extension_path = "."

      if target != "." && extension_name == "."
        raise ArgumentError, "Please, supply a extension name"
      elsif target == "."
        extension_path = File.expand_path(extension_name)
        extension_name = File.basename(extension_path)
      else
        extension_path = "#{target}/#{extension_name}"
      end
      [ extension_path, extension_name ]
    end

	def has_scm?(path)
		begin
			Repo.new(path)
		rescue Grit::InvalidGitRepositoryError
			false
		end
	end

	def scm_archive(path, archive, prefix, treeish='master')
		git = Git.new(Repo.new(path).path)
		git.archive({:format => "zip", :prefix => prefix, :output => archive}, treeish)
	end

	def zip_archive(path, archive, prefix)
		Zippy.create(archive) do |zip|
			Dir["#{path}/**/**"].each do |file|
				zip["#{prefix}#{file.sub(path+'/','')}"] = File.open(file) unless File.directory?(file)
			end
		end
	end

    def can_zip?(archive)
      can_zip = false

      if File.exists?(archive)
        say_status :conflict, archive, :red
        if yes? "Overwrite #{archive}? [yN]"
          can_zip = true
        else
          can_zip = false
        end
      else
        can_zip = true
      end
    end

    def is_extension?(dir=".")
      is_dir?(dir) && File.exists?("#{dir}/META.json")
    end

    def is_dir?(dir)
      File.directory?(dir)
    end

    def config_options
      file = File.join(target, "META.json")

      if File.exist?(file)
        @@config_options ||= JSON.load(File.read(file))
      else
        {}
      end
    end

    def set_accessors(extension_name="your_extension_name")
      self.extension_name = extension_name

      self.maintainer      = options[:maintainer]      || config_options["maintainer"]      || "The maintainer's name"
      self.abstract        = options[:abstract]        || config_options["abstract"]        || "A short description"
      self.license         = options[:license]         || config_options["license"]         || "postgresql"
      self.version         = options[:version]         || config_options["version"]         || "0.0.1"

      self.description     = options[:description]     || config_options["description"]     || "A long description"
      self.generated_by    = options[:generated_by]    || config_options["generated_by"]    || maintainer
      self.tags            = options[:tags]            || config_options["tags"]
      self.release_status  = options[:release_status]  || config_options["release_status"]  || "unstable"

      self.destination_root = target
    end
  end
end