Module: Gemnasium

Defined in:
lib/gemnasium.rb,
lib/gemnasium/errors.rb,
lib/gemnasium/options.rb,
lib/gemnasium/version.rb,
lib/gemnasium/connection.rb,
lib/gemnasium/configuration.rb,
lib/gemnasium/dependency_files.rb

Defined Under Namespace

Classes: Configuration, Connection, DependencyFiles, DeprecatedApiVersionError, InvalidApiKeyError, MalformedRequestError, MissingParamsError, NoSlotsAvailableError, NonBillableUserError, Options, ProjectNotFoundError, ProjectNotOfflineError, ProjectParamMissingError, UnsupportedDependencyFilesError

Constant Summary collapse

VERSION =
"3.2.1"

Class Method Summary collapse

Class Method Details

.configObject



215
216
217
# File 'lib/gemnasium.rb', line 215

def config
  @config || quit_because_of('No configuration file loaded')
end

.create_project(options) ⇒ Object

Create the project on Gemnasium

Parameters:

  • options (Hash)

    Parsed options from the command line. Options supported:

    • :project_path - Path to the project (required)


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
# File 'lib/gemnasium.rb', line 123

def create_project options
  @config = load_config(options[:project_path])
  ensure_config_is_up_to_date!

  if has_project_slug?
    quit_because_of("You already have a project slug refering to an existing project. Please remove this project slug from your configuration file to create a new project.")
  end

  project_params = { name: @config.project_name, branch: @config.project_branch}

  result = request("#{connection.api_path_for('projects')}", project_params)
  project_name, project_slug, remaining_slot_count = result.values_at 'name', 'slug', 'remaining_slot_count'

  notify "Project `#{ project_name }` successfully created.", :green
  notify "Project slug is `#{ project_slug }`.", :green
  notify "Remaining private slots: #{ remaining_slot_count }", :blue

  if @config.writable?
    @config.store_value!(:project_slug, project_slug, "This unique project slug has been set by `gemnasium create`.")
    notify "Your configuration file has been updated."
  else
    notify "Configuration file cannot be updated. Please edit the file and update the project slug manually."
  end

rescue => exception
  quit_because_of(exception.message)
end

.install(options) ⇒ Object

Install needed file(s) to run gemnasium command

Parameters:

  • options (Hash)

    Parsed options from the command line. Options supported:

    • :install_rake_task - Install a rake task to the project
    • :install_git_hook - Install a git post-commit hook
    • :project_path - Path to the project (required)


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
# File 'lib/gemnasium.rb', line 64

def install options
  require 'fileutils'

  # Install config file
  config_file_dir   = "#{options[:project_path]}/config"

  unless File.exists? config_file_dir
    notify "Creating config directory"
    FileUtils.mkdir_p config_file_dir
  end

  # Try to add config/gemnasium.yml to .gitignore
  if File.exists? "#{options[:project_path]}/.gitignore"
    File.open("#{options[:project_path]}/.gitignore", 'a+') do |f|
      f.write("\n# Gemnasium gem configuration file\nconfig/gemnasium.yml") unless f.read.include? 'config/gemnasium.yml'
      notify "Configuration file added to your project's .gitignore."
    end
  end

  notify 'Please fill configuration file with accurate values.', :blue if copy_template('gemnasium.yml', "#{config_file_dir}/gemnasium.yml")

  # Install git hook
  if options[:install_git_hook]
    notify ''
    if File.exists? "#{options[:project_path]}/.git/hooks"
      copy_template('post-commit', "#{options[:project_path]}/.git/hooks/post-commit")
    else
      notify "#{options[:project_path]} is not a git repository. Try to run `git init`.", :red
    end
  end

  # Install rake task
  if options[:install_rake_task]
    notify ''
    if !File.exists? "#{options[:project_path]}/Rakefile"
      notify "Rakefile not found.", :red
    else
      rake_file_dir = "#{options[:project_path]}/lib/tasks"

      unless File.exists? rake_file_dir
        notify "Creating lib/tasks directory"
        FileUtils.mkdir_p rake_file_dir
      end

      if copy_template('gemnasium.rake', "#{rake_file_dir}/gemnasium.rake")
        notify 'Usage:', :blue
        notify "\trake gemnasium:push \t\t- to push your dependency files", :blue
        notify "\trake gemnasium:create \t\t- to create your project on Gemnasium", :blue
        notify "\trake gemnasium:migrate \t\t- to migrate your configuration file", :blue
        notify "\trake gemnasium:resolve \t\t- to resolve the project name to an existing project", :blue
      end
    end
  end
end

.migrate(options) ⇒ Object

Migrate the configuration file

Parameters:

  • options (Hash)

    Parsed options from the command line. Options supported:

    • :project_path - Path to the project (required)


156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gemnasium.rb', line 156

def migrate options
  @config = load_config(options[:project_path])

  if @config.needs_to_migrate?
    @config.migrate!
    notify "Your configuration has been updated.", :green
    notify "Run `gemnasium resolve` if your config is related to an existing project."
    notify "Run `gemnasium create` if you want to create a new project on Gemnasium."
  else
    notify "Your configuration file is already up-to-date.", :green
  end
end

.push(options) ⇒ Object

Push dependency files to Gemnasium

Parameters:

  • options (Hash)

    Parsed options from the command line. Options supported:

    • :project_path - Path to the project (required)


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
# File 'lib/gemnasium.rb', line 13

def push options
  @config = load_config(options[:project_path])
  ensure_config_is_up_to_date!

  unless current_branch == @config.project_branch
    if options[:ignore_branch]
      notify "Not on tracked branch (#{@config.project_branch}), but ignore-branch option set to true. Continuing.\n", :blue
    elsif options[:silence_branch]
      notify "Not on tracked branch (#{@config.project_branch}). Exiting silently.\n", :blue
      return
    else
      quit_because_of("Not on tracked branch (#{@config.project_branch})")
    end
  end

  unless has_project_slug?
    quit_because_of('Project slug not defined. Please create a new project or "resolve" the name of an existing project.')
  end

  dependency_files_hashes = DependencyFiles.get_sha1s_hash(options[:project_path])
  quit_because_of("No supported dependency files detected.") if dependency_files_hashes.empty?
  notify "#{dependency_files_hashes.keys.count} supported dependency file(s) found: #{dependency_files_hashes.keys.join(', ')}"

  # Ask to Gemnasium server which dependency file should be uploaded (new or modified)
  comparison_results = request("#{connection.api_path_for('dependency_files')}/compare", dependency_files_hashes)
  files_to_upload = comparison_results['to_upload']
  deleted_files = comparison_results['deleted']
  notify "#{deleted_files.count} deleted file(s): #{deleted_files.join(', ')}", :blue unless deleted_files.empty?

  unless files_to_upload.empty?
    notify "#{files_to_upload.count} file(s) to upload: #{files_to_upload.join(', ')}"

    # Upload requested dependency files content
    upload_summary = request("#{connection.api_path_for('dependency_files')}/upload", DependencyFiles.get_content_to_upload(options[:project_path], files_to_upload))
    notify "Added dependency files: #{upload_summary['added']}", :green
    notify "Updated dependency files: #{upload_summary['updated']}", :green
    notify "Unchanged dependency files: #{upload_summary['unchanged']}", :blue unless upload_summary['unchanged'].empty?
    notify "Unsupported dependency files: #{upload_summary['unsupported']}", :blue unless upload_summary['unsupported'].empty?
  else
    notify "The project's dependency files are up-to-date.", :blue
  end
rescue => exception
  quit_because_of(exception.message)
end

.resolve_project(options) ⇒ Object

Find and store the project slug matching the given project name.

Parameters:

  • options (Hash)

    Parsed options from the command line. Options supported:

    • :project_path - Path to the project (required)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gemnasium.rb', line 174

def resolve_project options
  @config = load_config(options[:project_path])
  ensure_config_is_up_to_date!

  # REFACTOR: similar code in #create_project
  if has_project_slug?
    quit_because_of("You already have a project slug refering to an existing project. Please remove this project slug from your configuration file.")
  end

  project_name =  @config.project_name
  project_branch =  @config.project_branch || 'master'
  projects = request("#{connection.api_path_for('projects')}")

  candidates = projects.select do |project|
    project['name'] == project_name && project['origin'] == 'offline' && project['branch'] == project_branch
  end

  criterias = "name `#{ project_name }` and branch `#{ project_branch }`"
  if candidates.size == 0
    quit_because_of("You have no off-line project matching #{ criterias }.")
  elsif candidates.size > 1
    quit_because_of("You have more than one off-line project matching #{ criterias }.")
  end

  project = candidates.first
  project_slug = project['slug']
  notify "Project slug is `#{ project_slug }`.", :green

  # REFACTOR: similar code in #create_project
  if @config.writable?
    @config.store_value!(:project_slug, project_slug, "This unique project slug has been set by `gemnasium resolve`.")
    notify "Your configuration file has been updated."
  else
    notify "Configuration file cannot be updated. Please edit the file and update the project slug manually."
  end

rescue => exception
  quit_because_of(exception.message)
end