diff options
author | Zeger-Jan van de Weg <zegerjan@gitlab.com> | 2016-04-29 16:25:03 +0200 |
---|---|---|
committer | Alfredo Sumaran <alfredo@gitlab.com> | 2016-05-20 15:58:36 -0500 |
commit | e166a8022a3f239938a1449a0a8ce3485f309766 (patch) | |
tree | af9f612f599b01f5736e7b439f9579d77658f156 /lib | |
parent | 56eb42007ae8c3c390b35bf336884b3bad3591c5 (diff) | |
download | gitlab-ce-e166a8022a3f239938a1449a0a8ce3485f309766.tar.gz |
Backend for a gitignores dropdown
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/entities.rb | 8 | ||||
-rw-r--r-- | lib/api/gitignores.rb | 29 | ||||
-rw-r--r-- | lib/gitlab/gitignore.rb | 68 | ||||
-rw-r--r-- | lib/tasks/gitlab/update_gitignore.rake | 26 |
5 files changed, 132 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 360fb41a721..6cd909f6115 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -58,5 +58,6 @@ module API mount ::API::Runners mount ::API::Licenses mount ::API::Subscriptions + mount ::API::Gitignores end end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 8298e3ad34c..31491cf31dd 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -457,5 +457,13 @@ module API expose(:limitations) { |license| license.meta['limitations'] } expose :content end + + class GitignoresList < Grape::Entity + expose :name + end + + class Gitignore < Grape::Entity + expose :name, :content + end end end diff --git a/lib/api/gitignores.rb b/lib/api/gitignores.rb new file mode 100644 index 00000000000..1af9ba6b316 --- /dev/null +++ b/lib/api/gitignores.rb @@ -0,0 +1,29 @@ +module API + class Gitignores < Grape::API + + # Get the list of the available gitignore templates + # + # Example Request: + # GET /gitignores + get 'gitignores' do + present Gitlab::Gitignore.all, with: Entities::GitignoresList + end + + # Get the text for a specific gitignore + # + # Parameters: + # key (required) - The key of a license + # + # Example Request: + # GET /gitignores/elixir + # + get 'gitignores/:key' do + required_attributes! [:key] + + gitignore = Gitlab::Gitignore.find(params[:key]) + not_found!('.gitignore') unless gitignore + + present gitignore, with: Entities::Gitignore + end + end +end diff --git a/lib/gitlab/gitignore.rb b/lib/gitlab/gitignore.rb new file mode 100644 index 00000000000..a2de2831e38 --- /dev/null +++ b/lib/gitlab/gitignore.rb @@ -0,0 +1,68 @@ +module Gitlab + class Gitignore + FILTER_REGEX = /\.gitignore\z/.freeze + + attr_accessor :name, :directory + + def initialize(name, directory) + @name = name + @directory = directory + end + + def content + File.read(path) + end + + class << self + def all + languages_frameworks + global + end + + def find(key) + file_name = "#{key}.gitignore" + + directory = select_directory(file_name) + directory ? new(key, directory) : nil + end + + def global + files_for_folder(global_dir).map { |f| new(f, global_dir) } + end + + def languages_frameworks + files_for_folder(gitignore_dir).map { |f| new(f, gitignore_dir) } + end + end + + private + + def path + File.expand_path("#{name}.gitignore", directory) + end + + class << self + def select_directory(file_name) + [self.gitignore_dir, self.global_dir].find { |dir| File.exist?(File.expand_path(file_name, dir)) } + end + + def global_dir + File.expand_path('Global', gitignore_dir) + end + + def gitignore_dir + File.expand_path('vendor/gitignore', Rails.root) + end + + def files_for_folder(dir) + gitignores = [] + Dir.entries(dir).each do |e| + next unless e.end_with?('.gitignore') + + gitignores << e.gsub(FILTER_REGEX, '') + end + + gitignores + end + end + end +end diff --git a/lib/tasks/gitlab/update_gitignore.rake b/lib/tasks/gitlab/update_gitignore.rake new file mode 100644 index 00000000000..61cbfd6737d --- /dev/null +++ b/lib/tasks/gitlab/update_gitignore.rake @@ -0,0 +1,26 @@ +namespace :gitlab do + desc "GitLab | Update gitignore" + task :update_gitignore do + dir = File.expand_path('vendor', Rails.root) + FileUtils.cd(dir) + + dir = File.expand_path('gitignore', dir) + clone_gitignores(dir) + remove_unneeded_files(dir) + + puts "Done".green + end + + def clone_gitignores(dir) + FileUtils.rm_rf(dir) if Dir.exist?(dir) + system('git clone --depth=1 --branch=master https://github.com/github/gitignore.git') + end + + def remove_unneeded_files(dir) + [File.expand_path('Global', dir), dir].each do |path| + Dir.entries(path).reject { |e| e =~ /(\.{1,2}|Global|\.gitignore)\z/ }.each do |file| + FileUtils.rm_rf File.expand_path(file, path) + end + end + end +end |