From bc240aee52db2cffd77f9c0df84ff6f77b4d7a26 Mon Sep 17 00:00:00 2001 From: Henrik Feldt Date: Sat, 13 Feb 2016 19:15:03 +0100 Subject: [PATCH] [nuget_model] can generate simple paket file template Ref #174 --- lib/albacore/nuget_model.rb | 59 ++++++++++++++++++++++++++---- spec/nuget_model_spec.rb | 72 ++++++++++++++++++++++++------------- 2 files changed, 100 insertions(+), 31 deletions(-) diff --git a/lib/albacore/nuget_model.rb b/lib/albacore/nuget_model.rb index 5c102ebe..a6cf7cd9 100644 --- a/lib/albacore/nuget_model.rb +++ b/lib/albacore/nuget_model.rb @@ -45,7 +45,7 @@ def self.nuspec_field *syms @#{sym} = val @set_fields.add? :#{sym} end}) - end + end end # REQUIRED: gets or sets the id of this package @@ -153,6 +153,34 @@ def to_xml to_xml_builder.to_xml end + def to_paket_template + fields = @set_fields.inject("type file") do |state, field| + txt = case field + when :release_notes + "#{send(field).split(/\n/).inject("") { |acc, val| + "#{acc}\n #{val}" + }}" + else + " #{send(field)}" + end + + "#{state}\n#{Metadata.camel_case(field)}#{txt}" + end + + fws = @framework_assemblies || [] + fws = fws.empty? ? "" : fws.inject("\nframeworkDependencies") do |s, asm| + _, asm = asm + "#{s}\n #{asm.id}" + end + + dependencies = @dependencies.inject("\ndependencies") do |state, d| + _, dependency = d + "#{state}\n #{dependency.id} ~> #{dependency.version}" + end unless @dependencies.empty? + + "#{fields}#{dependencies}#{fws}" + end + def merge_with other raise ArgumentError, 'other is nil' if other.nil? raise ArgumentError, 'other is wrong type' unless other.is_a? Metadata @@ -166,13 +194,13 @@ def merge_with other # set all my fields to the new instance @set_fields.each do |field| - debug "setting field '#{field}' to be '#{send(field)}' [nuget model: metadata]" + debug "setting field '#{field}' to be '#{send(field)}' [nuget model: metadata]" m_next.send(:"#{field}=", send(field)) end # set all other's fields to the new instance, overriding mine other.set_fields.each do |field| - debug "setting field '#{field}' to be '#{send(field)}' [nuget model: metadata]" + debug "setting field '#{field}' to be '#{send(field)}' [nuget model: metadata]" m_next.send(:"#{field}=", other.send(field)) end @@ -189,11 +217,11 @@ def self.from_xml node m = Metadata.new node.children.reject { |n| n.text? }.each do |n| if n.name == 'dependencies' - n.children.reject { |n| n.text? }.each do |dep| + n.children.reject { |nn| nn.text? }.each do |dep| m.add_dependency dep['id'], dep['version'] end elsif n.name == 'frameworkDepdendencies' - n.children.reject { |n| n.text? }.each do |dep| + n.children.reject { |nn| nn.text? }.each do |dep| m.add_framework_depdendency dep['id'], dep['version'] end else @@ -210,6 +238,14 @@ def self.pascal_case str :"#{str}" end + def self.camel_case str + str.to_s. + split('_'). + inject([]) { |buffer, e| + buffer.push(buffer.empty? ? e : e.capitalize) + }.join + end + def self.underscore str str.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). @@ -284,6 +320,15 @@ def to_xml to_xml_builder.to_xml end + # Converts the Package instance to a paket template string + def to_paket_template + files = @files.inject("\nfiles") do |state, file| + "#{state}\n #{file.src} => #{file.target}" + end + + %{#{metadata.to_paket_template}#{files}} + end + # creates a new Package/Metadata by overriding data in this instance with # data from passed instance def merge_with other @@ -390,7 +435,7 @@ def self.from_xxproj proj, *opts compile_files.each do |f| target = %W[src #{Albacore::Paths.normalise_slashes(f.include)}].join(Albacore::Paths.separator) package.add_file f.include, target - end + end debug "add dll and pdb files [nuget model: package]" package.add_file(Albacore::Paths.normalise_slashes(output + proj.asmname + '.pdb'), target_lib) @@ -400,7 +445,7 @@ def self.from_xxproj proj, *opts # add *.{dll,xml,config} %w[dll xml config pdb dll.mdb].each do |ext| file = %W{#{output} #{proj.asmname}.#{ext}}. - map { |f| f.gsub /\\$/, '' }. + map { |f| f.gsub(/\\$/, '') }. map { |f| Albacore::Paths.normalise_slashes f }. join(Albacore::Paths.separator) debug "adding binary file #{file} [nuget model: package]" diff --git a/spec/nuget_model_spec.rb b/spec/nuget_model_spec.rb index af1187be..65322c48 100644 --- a/spec/nuget_model_spec.rb +++ b/spec/nuget_model_spec.rb @@ -14,7 +14,7 @@ end describe "when adding a dependency" do - before do + before do subject.add_dependency 'DepId', '=> 3.4.5' end let :dep do @@ -33,7 +33,7 @@ describe "when adding a framework dependency" do before do - subject.add_framework_dependency 'System.Transactions', '2.0.0' + subject.add_framework_dependency 'System.Transactions', '2.0.0' end let :dep do subject.framework_assemblies.first[1] @@ -69,33 +69,57 @@ describe Albacore::NugetModel::Package, "when doing paket template generation" do let :metadata do - md = Albacore::NuGetModel::Metadata.new + md = ::Albacore::NugetModel::Metadata.new md.id = 'MyExamplePackage' md.title = 'My Example Package' - md.title - md.add_ + md.version = '1.0.3' + md.authors = 'Henrik Feldt' + md.owners = 'Henrik Feldt Owner' + md.summary = 'If there was a man, or a woman, who would withstand.' + md.description = 'Wowowow' + md.release_notes = %{These are my notes, to those who came after me. + - Item 1 + - Item 2} + md.add_dependency 'FSharp.Core', '4.3.1' + md.add_framework_dependency 'System.Xml', '4.0.0' + md + end + + let :package do + pkg = ::Albacore::NugetModel::Package.new metadata + pkg.add_file 'bin/Release', 'lib/net45' + pkg end + let :expected do %{type file id MyExamplePackage title My Example Package version 1.0.3 authors Henrik Feldt -owners Henrik Feldt +owners Henrik Feldt Owner summary If there was a man, or a woman, who would withstand. -description - Wowowow -files - bin/Release => lib +description Wowowow releaseNotes - These are my notes, to those who come after me. - + These are my notes, to those who came after me. - Item 1 - Item 2 dependencies - FSharp.Core >= 4.3.1 -} + FSharp.Core ~> 4.3.1 +frameworkDependencies + System.Xml +files + bin/Release => lib/net45} + end + + subject do + package.to_paket_template end + + it 'should generate sweet paket templates' do + expect(subject).to eq(expected) + end + end describe Albacore::NugetModel::Package, "from XML" do @@ -124,8 +148,8 @@ - - + + @@ -174,7 +198,7 @@ parser.xpath('./ng:metadata/ng:dependencies', ns).children.reject{ |c| c.text? }.each do |dep| expect(subject.metadata.dependencies[dep['id']]).to_not be_nil end - end + end end it "should have all the files of the XML above" do @@ -233,7 +257,7 @@ let :projfile do curr = File.dirname(__FILE__) File.join curr, "testdata", "Project", "ProjectWithTitle.fsproj" - end + end subject do Albacore::NugetModel::Package.from_xxproj_file projfile @@ -267,7 +291,7 @@ end p.add_file 'CodeFolder/A.cs', 'lib/CodeFolder/A.cs' p.add_file 'CodeFolder/*.fs', 'lib', 'AssemblyInfo.fs' - end + end let :p2 do p = Albacore::NugetModel::Package.new.with_metadata do |m| m.id = 'A.B.C' @@ -302,7 +326,7 @@ let :projfile do curr = File.dirname(__FILE__) File.join curr, "testdata", "TestingDependencies", "Sample.Commands", "Sample.Commands.fsproj" - end + end subject do Albacore::NugetModel::Package.from_xxproj_file projfile, @@ -343,7 +367,7 @@ curr = File.dirname(__FILE__) File.join curr, "testdata", "TestingDependencies", "Sample.Commands", "Sample.Commands.fsproj" - end + end let :opts do { project_dependencies: false, @@ -444,10 +468,10 @@ let :projfile do curr = File.dirname(__FILE__) File.join curr, "testdata", "EmptyProject", "EmptyProject.csproj" - end + end has_not_file 'bin/Debug/Sample.Commands.dll' - has_not_file 'bin/Debug/EmptyProject.dll' - has_not_file 'bin/Debug/EmptyProject.xml' + has_not_file 'bin/Debug/EmptyProject.dll' + has_not_file 'bin/Debug/EmptyProject.xml' has_file 'bin/Release/EmptyProject.dll', 'lib/net45' end end