Skip to content

Commit

Permalink
Merge pull request #3 from oudirichi/master
Browse files Browse the repository at this point in the history
create language directory when language does not exist
  • Loading branch information
cognibox authored Oct 16, 2017
2 parents 8090295 + 1aa49a7 commit 7822dbc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
19 changes: 18 additions & 1 deletion lib/cbx_loco/loco_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ def self.import
api_ext = fmt[:api_ext]
tag = CbxLoco.asset_tag i18n_file[:id], i18n_file[:name]

print "Importing \"#{language}\" #{tag} assets... "

api_params = { filter: tag, order: :id }
case i18n_file[:format]
Expand All @@ -180,6 +179,10 @@ def self.import

translations = get "export/locale/#{language}.#{api_ext}", api_params, false

dirname = File.dirname(file_path)
create_directory(dirname) unless File.directory?(dirname)

print "Importing \"#{language}\" #{tag} assets... "
f = File.new file_path, "w:UTF-8"
f.write translations.force_encoding("UTF-8")
f.close
Expand All @@ -203,10 +206,24 @@ def self.import

private

def self.create_directory(path)
print "Creating \"#{path}\" folder... "

FileUtils.mkdir_p(path)
puts "Done!".colorize(:green)

print "Creating \".keep\" file... "
file_path = File.join path, ".keep"
FileUtils.touch(file_path)

puts "Done!".colorize(:green)
end

def self.print_error(message)
puts "\n\n" + message.colorize(:red).bold
end


def self.run_event(event_name)
CbxLoco.configuration.tasks.with_indifferent_access

Expand Down
2 changes: 1 addition & 1 deletion lib/cbx_loco/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CbxLoco
VERSION = "1.0.0"
VERSION = "1.0.1"
end
41 changes: 23 additions & 18 deletions spec/cbx_loco/loco_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,6 @@ def create_files
f = File.new file_path, "w:UTF-8"
f.write @fake_translations[i18n_file[:id]].force_encoding("UTF-8")
f.close

# create dst dir
@fake_languages.each do |language|
case i18n_file[:format]
when :gettext
file_path = CbxLoco.file_path fmt[:path], language, [i18n_file[:name], fmt[:dst_ext]].join(".")
when :yaml
file_path = CbxLoco.file_path fmt[:path], [i18n_file[:name], language, fmt[:dst_ext]].join(".")
end

dirname = File.dirname(file_path)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
file_path = CbxLoco.file_path dirname, ".keep"
f = File.new file_path, "w:UTF-8"
f.close
end
end
end
end

Expand Down Expand Up @@ -315,6 +297,29 @@ def delete_files
CbxLoco::LocoAdapter.import
end

context "when locale folder does not exist" do
let!(:mock_file) { double }
let(:folder_to_create) { "./locale/en" }
let(:keep_file) { File.join(folder_to_create, ".keep") }

before do
allow(File).to receive(:directory?).and_return(true)
allow(File).to receive(:directory?).with(folder_to_create).and_return(false)
allow(FileUtils).to receive(:mkdir_p).with(folder_to_create)
allow(FileUtils).to receive(:touch)
end

it "should create the folder" do
CbxLoco::LocoAdapter.import
expect(FileUtils).to have_received(:mkdir_p).with(folder_to_create)
end

it "should create .keep file" do
CbxLoco::LocoAdapter.import
expect(FileUtils).to have_received(:touch).with(keep_file)
end
end

it "should write API return in language files" do
CbxLoco::LocoAdapter.import

Expand Down
2 changes: 1 addition & 1 deletion spec/cbx_loco/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

describe CbxLoco::VERSION do
it "should return the version number" do
expect(CbxLoco::VERSION).to eq "1.0.0"
expect(CbxLoco::VERSION).to eq "1.0.1"
end
end

0 comments on commit 7822dbc

Please sign in to comment.