Skip to content

Commit

Permalink
[nuget_model] can generate simple paket file template
Browse files Browse the repository at this point in the history
Ref #174
  • Loading branch information
haf committed Nov 4, 2016
1 parent 85a7b6a commit da0b280
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 31 deletions.
59 changes: 52 additions & 7 deletions lib/albacore/nuget_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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').
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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]"
Expand Down
72 changes: 48 additions & 24 deletions spec/nuget_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -124,8 +148,8 @@
<files>
<file src="Full/bin/Debug/*.dll" target="lib/net45"/>
<file src="Full/bin/Debug/*.pdb" target="lib/net45"/>
<file src="Silverlight/bin/Debug/*.dll" target="lib/sl40"/>
<file src="Silverlight/bin/Debug/*.pdb" target="lib/sl40"/>
<file src="Silverlight/bin/Debug/*.dll" target="lib/sl40"/>
<file src="Silverlight/bin/Debug/*.pdb" target="lib/sl40"/>
<file src="**/*.cs" target="src"/>
</files>
</package>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit da0b280

Please sign in to comment.