Skip to content

Commit

Permalink
Added new Build actions (Page & Resource) for WPF support
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-tihon committed Mar 24, 2016
1 parent e923c27 commit 25d9657
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
49 changes: 36 additions & 13 deletions src/Paket.Core/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ open Paket.Requirements
[<RequireQualifiedAccess>]
type BuildAction =
| Compile | Content | Reference
| Resource | Page // These two are WPF only - https://msdn.microsoft.com/library/aa970494%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396
// There are some other build actions - http://stackoverflow.com/questions/145752/what-are-the-various-build-action-settings-in-visual-studio-project-properties/145769#145769

override this.ToString() =
match this with
| Compile -> "Compile"
| Content -> "Content"
| Reference -> "Reference"
| Resource -> "Resource"
| Page -> "Page"

static member PaketFileNodeNames =
[Compile; Content; Resource; Page] |> List.map (fun x->x.ToString())

/// File item inside of project files.
type FileItem =
Expand Down Expand Up @@ -545,11 +552,15 @@ module ProjectFile =
| None ->
[BuildAction.Content, createNode "ItemGroup" project
BuildAction.Compile, createNode "ItemGroup" project
BuildAction.Reference, createNode "ItemGroup" project ]
BuildAction.Reference, createNode "ItemGroup" project
BuildAction.Resource, createNode "ItemGroup" project
BuildAction.Page, createNode "ItemGroup" project ]
| Some node ->
[BuildAction.Content, node :?> XmlElement
BuildAction.Compile, node :?> XmlElement
BuildAction.Reference, node :?> XmlElement ]
BuildAction.Compile, node :?> XmlElement
BuildAction.Reference, node :?> XmlElement
BuildAction.Resource, node :?> XmlElement
BuildAction.Page, node :?> XmlElement ]
|> dict

for fileItem in fileItems |> List.rev do
Expand All @@ -569,6 +580,10 @@ module ProjectFile =
node
|> addChild (createNodeSet "HintPath" fileItem.Include project)
|> addChild (createNodeSet "Private" "True" project)
| BuildAction.Page ->
node
|> addChild (createNodeSet "Generator" "MSBuild:Compile" project)
|> addChild (createNodeSet "SubType" "Designer" project)
| _ -> node
|> fun n -> if fileItem.WithPaketSubNode then addChild (createNodeSet "Paket" "True" project) n else n
|> fun n -> match fileItem.CopyToOutputDirectory with
Expand Down Expand Up @@ -611,11 +626,12 @@ module ProjectFile =
| None ->
let firstNode = fileItemsInSameDir |> Seq.head
firstNode.ParentNode.InsertBefore(libReferenceNode, firstNode) |> ignore

let paketNodes =
(findPaketNodes "Compile" project)
@ (findPaketNodes "Content" project)


let paketNodes =
BuildAction.PaketFileNodeNames
|> List.map (fun name -> findPaketNodes name project)
|> List.concat

// remove unneeded files
for paketNode in paketNodes do
match getAttribute "Include" paketNode with
Expand Down Expand Up @@ -969,8 +985,9 @@ module ProjectFile =
File.SetLastWriteTimeUtc(project.FileName, DateTime.UtcNow)

let getPaketFileItems project =
findPaketNodes "Content" project
|> List.append <| findPaketNodes "Compile" project
BuildAction.PaketFileNodeNames
|> List.map (fun name -> findPaketNodes name project)
|> List.concat
|> List.map (fun n -> FileInfo(Path.Combine(Path.GetDirectoryName project.FileName, n.Attributes.["Include"].Value)))

let getProjectGuid project =
Expand Down Expand Up @@ -1122,9 +1139,15 @@ module ProjectFile =
|> Option.iter (fun n -> n.ParentNode.RemoveChild n |> ignore)

let determineBuildAction fileName (project:ProjectFile) =
if Path.GetExtension project.FileName = Path.GetExtension fileName + "proj"
then BuildAction.Compile
else BuildAction.Content
match (Path.GetExtension fileName).ToLowerInvariant() with
| ext when Path.GetExtension project.FileName = ext + "proj"
-> BuildAction.Compile
| ".xaml" -> BuildAction.Page
| ".ttf" | ".png" | ".ico" | ".jpg" | ".jpeg"| ".bmp" | ".gif"
| ".wav" | ".mid" | ".midi"| ".wma" | ".mp3" | ".ogg" | ".rma"
| ".avi" | ".mp4" | ".divx"| ".wmv" //TODO: and other media types
-> BuildAction.Resource
| _ -> BuildAction.Content

let determineBuildActionForRemoteItems fileName (project:ProjectFile) =
if Path.GetExtension fileName = ".dll"
Expand Down
18 changes: 16 additions & 2 deletions tests/Paket.Tests/ProjectFile/FileBuildActionSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,19 @@ let ``should recognize compilable files``() =
let ``should recognize content files``() =
(createProject "A.csproj").DetermineBuildAction "Something.js" |> shouldEqual BuildAction.Content
(createProject "B.fsproj").DetermineBuildAction "config.yml" |> shouldEqual BuildAction.Content
(createProject "C.vbproj").DetermineBuildAction "noext" |> shouldEqual BuildAction.Content
(createProject "D.nproj").DetermineBuildAction "App.config" |> shouldEqual BuildAction.Content
(createProject "C.vbproj").DetermineBuildAction "noext" |> shouldEqual BuildAction.Content
(createProject "D.nproj").DetermineBuildAction "App.config" |> shouldEqual BuildAction.Content

[<Test>]
let ``should recognize page files``() =
(createProject "A.csproj").DetermineBuildAction "Form1.xaml" |> shouldEqual BuildAction.Page
(createProject "B.fsproj").DetermineBuildAction "Form1.Xaml" |> shouldEqual BuildAction.Page
(createProject "C.vbproj").DetermineBuildAction "Form1.XAML" |> shouldEqual BuildAction.Page
(createProject "D.nproj" ).DetermineBuildAction "Form1.XaML" |> shouldEqual BuildAction.Page

[<Test>]
let ``should recognize resource files``() =
(createProject "A.csproj").DetermineBuildAction "Form1.ttf" |> shouldEqual BuildAction.Resource
(createProject "B.fsproj").DetermineBuildAction "Form1.ico" |> shouldEqual BuildAction.Resource
(createProject "C.vbproj").DetermineBuildAction "Form1.png" |> shouldEqual BuildAction.Resource
(createProject "D.nproj" ).DetermineBuildAction "Form1.jpg" |> shouldEqual BuildAction.Resource
4 changes: 2 additions & 2 deletions tests/Paket.Tests/ProjectFile/OutputSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let ``should maintain order when updating project file items`` () =
let rec nodes node =
seq {
for node in node |> Seq.cast<XmlNode> do
if node.Name ="Compile" || node.Name = "Content"
if List.contains node.Name BuildAction.PaketFileNodeNames
then yield Paket.Xml.getAttribute "Include" node
yield! nodes node
}
Expand Down Expand Up @@ -120,7 +120,7 @@ let ``should remove missing files that exist in the project`` () =
let rec nodes node =
seq {
for node in node |> Seq.cast<XmlNode> do
if node.Name ="Compile" || node.Name = "Content"
if List.contains node.Name BuildAction.PaketFileNodeNames
then yield Paket.Xml.getAttribute "Include" node
yield! nodes node
}
Expand Down

0 comments on commit 25d9657

Please sign in to comment.