Commit 79620c50 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg Committed by Alfredo Sumaran

Update API and fetching task

parent 1f5fcb63
...@@ -2,15 +2,16 @@ module Gitlab ...@@ -2,15 +2,16 @@ module Gitlab
class Gitignore class Gitignore
FILTER_REGEX = /\.gitignore\z/.freeze FILTER_REGEX = /\.gitignore\z/.freeze
attr_accessor :name, :directory def initialize(path)
@path = path
end
def initialize(name, directory) def name
@name = name File.basename(@path, '.gitignore')
@directory = directory
end end
def content def content
File.read(path) File.read(@path)
end end
class << self class << self
...@@ -22,46 +23,33 @@ module Gitlab ...@@ -22,46 +23,33 @@ module Gitlab
file_name = "#{key}.gitignore" file_name = "#{key}.gitignore"
directory = select_directory(file_name) directory = select_directory(file_name)
directory ? new(key, directory) : nil directory ? new(File.join(directory, file_name)) : nil
end end
def global def global
files_for_folder(global_dir).map { |f| new(f, global_dir) } files_for_folder(global_dir).map { |file| new(File.join(global_dir, file)) }
end end
def languages_frameworks def languages_frameworks
files_for_folder(gitignore_dir).map { |f| new(f, gitignore_dir) } files_for_folder(gitignore_dir).map { |file| new(File.join(gitignore_dir, file)) }
end end
end
private private
def path
File.expand_path("#{name}.gitignore", directory)
end
class << self
def select_directory(file_name) def select_directory(file_name)
[self.gitignore_dir, self.global_dir].find { |dir| File.exist?(File.expand_path(file_name, dir)) } [gitignore_dir, global_dir].find { |dir| File.exist?(File.join(dir, file_name)) }
end end
def global_dir def global_dir
File.expand_path('Global', gitignore_dir) File.join(gitignore_dir, 'Global')
end end
def gitignore_dir def gitignore_dir
File.expand_path('vendor/gitignore', Rails.root) Rails.root.join('vendor/gitignore')
end end
def files_for_folder(dir) def files_for_folder(dir)
gitignores = [] Dir.glob("#{dir.to_s}/*.gitignore").map { |file| file.gsub(FILTER_REGEX, '') }
Dir.entries(dir).each do |e|
next unless e.end_with?('.gitignore')
gitignores << e.gsub(FILTER_REGEX, '')
end
gitignores
end end
end end
end end
......
namespace :gitlab do namespace :gitlab do
desc "GitLab | Update gitignore" desc "GitLab | Update gitignore"
task :update_gitignore do task :update_gitignore do
dir = File.expand_path('vendor', Rails.root) unless clone_gitignores
FileUtils.cd(dir) puts "Cloning the gitignores failed".red
return
end
dir = File.expand_path('gitignore', dir) remove_unneeded_files(gitignore_directory)
clone_gitignores(dir) remove_unneeded_files(global_directory)
remove_unneeded_files(dir)
puts "Done".green puts "Done".green
end end
def clone_gitignores(dir) def clone_gitignores
FileUtils.rm_rf(dir) if Dir.exist?(dir) FileUtils.rm_rf(gitignore_directory) if Dir.exist?(gitignore_directory)
FileUtils.cd vendor_directory
system('git clone --depth=1 --branch=master https://github.com/github/gitignore.git') system('git clone --depth=1 --branch=master https://github.com/github/gitignore.git')
end end
def remove_unneeded_files(dir) # Retain only certain files:
[File.expand_path('Global', dir), dir].each do |path| # - The LICENSE, because we have to
Dir.entries(path).reject { |e| e =~ /(\.{1,2}|Global|\.gitignore)\z/ }.each do |file| # - The sub dir global
FileUtils.rm_rf File.expand_path(file, path) # - The gitignores themself
end # - Dir.entires returns also the entries '.' and '..'
def remove_unneeded_files(path)
Dir.foreach(path) do |file|
FileUtils.rm_rf(File.join(path, file)) unless file =~ /(\.{1,2}|LICENSE|Global|\.gitignore)\z/
end end
end end
private
def vendor_directory
Rails.root.join('vendor')
end
def gitignore_directory
File.join(vendor_directory, 'gitignore')
end
def global_directory
File.join(gitignore_directory, 'Global')
end
end end
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Gitlab::Gitignore do describe Gitlab::Gitignore do
subject { Gitlab::Gitignore } subject { Gitlab::Gitignore }
describe '.all' do describe '.all' do
it 'strips the gitignore suffix' do it 'strips the gitignore suffix' do
expect(subject.all.first.name).not_to end_with('.gitignore') expect(subject.all.first.name).not_to end_with('.gitignore')
...@@ -24,12 +25,13 @@ describe Gitlab::Gitignore do ...@@ -24,12 +25,13 @@ describe Gitlab::Gitignore do
ruby = subject.find('Ruby') ruby = subject.find('Ruby')
expect(ruby).to be_a Gitlab::Gitignore expect(ruby).to be_a Gitlab::Gitignore
expect(ruby.name).to eq('Ruby')
end end
end end
describe '#content' do describe '#content' do
it 'loads the full file' do it 'loads the full file' do
gitignore = subject.new('Ruby', File.expand_path('vendor/gitignore', Rails.root)) gitignore = subject.new(Rails.root.join('vendor/gitignore/Ruby.gitignore'))
expect(gitignore.name).to eq 'Ruby' expect(gitignore.name).to eq 'Ruby'
expect(gitignore.content).to start_with('*.gem') expect(gitignore.content).to start_with('*.gem')
......
...@@ -13,8 +13,8 @@ describe API::Gitignores, api: true do ...@@ -13,8 +13,8 @@ describe API::Gitignores, api: true do
describe 'Entity GitignoresList' do describe 'Entity GitignoresList' do
before { get api('/gitignores') } before { get api('/gitignores') }
it { expect(json_response.first['name']).to be_truthy } it { expect(json_response.first['name']).not_to be_nil }
it { expect(json_response.first['content']).to be_falsey } it { expect(json_response.first['content']).to be_nil }
end end
describe 'GET /gitignores' do describe 'GET /gitignores' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment