Module: PgxnUtils::NoTasks

Includes:
Grit, Constants
Included in:
CLI
Defined in:
lib/pgxn_utils/no_tasks.rb

Constant Summary

Constants included from Constants

Constants::UPLOAD_URL

Instance Method Summary collapse

Instance Method Details

#ask_for_pgxn_credentialObject



53
54
55
56
# File 'lib/pgxn_utils/no_tasks.rb', line 53

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

#can_zip?(archive) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pgxn_utils/no_tasks.rb', line 142

def can_zip?(archive)
  can_zip = false

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

#check_response(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pgxn_utils/no_tasks.rb', line 58

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

#config_optionsObject



166
167
168
169
170
171
172
173
174
# File 'lib/pgxn_utils/no_tasks.rb', line 166

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

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

#has_scm?(path) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/pgxn_utils/no_tasks.rb', line 121

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

#init_repository(extension_dir) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pgxn_utils/no_tasks.rb', line 21

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

#is_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/pgxn_utils/no_tasks.rb', line 162

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

#is_dirty?(extension_dir) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/pgxn_utils/no_tasks.rb', line 42

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

#is_extension?(dir = ".") ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/pgxn_utils/no_tasks.rb', line 158

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

#make_dist_clean(path) ⇒ Object



47
48
49
50
51
# File 'lib/pgxn_utils/no_tasks.rb', line 47

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

#prepare_multipart_post_for(filename) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/pgxn_utils/no_tasks.rb', line 74

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

#resolve_extension_path_and_name(extension_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pgxn_utils/no_tasks.rb', line 106

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

#scm_archive(path, archive, prefix, treeish = 'master') ⇒ Object



129
130
131
132
# File 'lib/pgxn_utils/no_tasks.rb', line 129

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

#selected_templateObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pgxn_utils/no_tasks.rb', line 7

def selected_template
	template = options[:template]

	unless [ 'sql', 'c', 'fdw' ].include?(template)
		if Dir["#{template}/*"].include?("#{template}/META.json") or
		   Dir["#{template}/*"].include?("#{template}/META.json.tt")
			template = File.expand_path(options[:template])
		else
			raise "invalid template: #{template}"
		end
	end
	template
end

#send_file_to_pgxn(filename) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/pgxn_utils/no_tasks.rb', line 97

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

#set_accessors(extension_name = "your_extension_name") ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pgxn_utils/no_tasks.rb', line 176

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

#try_send_file(request, filename) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pgxn_utils/no_tasks.rb', line 84

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

#zip_archive(path, archive, prefix) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/pgxn_utils/no_tasks.rb', line 134

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