-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.ps1
148 lines (132 loc) · 6.09 KB
/
convert.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# https://raw.githubusercontent.com/wgtmpeters/nugetprojectjson/master/ConvertProjectJson/create_project_json.ps1
# Script to generate project.json for all packages.config file in the solution.
# Run from root solution folder
# execute this script with powershell:
# powershell -File <filepath>
# [-f <targetframework] net4
Param
(
[alias("f")]
[string] $TargetFramework = "net4",
[alias("r")]
[switch]
$redo
)
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir = Split-Path -Parent $ScriptPath
$PackagesConfigFileName = 'packages.config'
if($redo){
$PackagesConfigFileName = 'packages_old.config'
}
#Create project.json from packages.config
Get-ChildItem -path '.' -Recurse -Include $PackagesConfigFileName |
ForEach {
$PackageFilePath = $_.FullName
$ProjectFilePath = $_.Directory.FullName + '\project.json'
Write-Output $PackageFilePath
Write-Output $ProjectFilePath
'{
"dependencies": {' | Out-File $ProjectFilePath
(Select-xml -xpath '//package' -Path $PackageFilePath | % { """{0}"": ""{1}""" -f $_.Node.id,$_.Node.version }) -join ",`n" | Out-File $ProjectFilePath -Append
' },
"frameworks": {
"' + $TargetFramework + '": {}
},
"runtimes": {
"win-anycpu": {},
"win": {}
}
}' | Out-File $ProjectFilePath -Append
Rename-Item -Path $PackageFilePath -NewName 'packages_old.config'
}
$MsbNS = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
Get-ChildItem -path '.' -Recurse -Include '*.csproj' | ForEach {
$CsProjFilePath = $_.FullName
$ProjectFilePath = $_.Directory.FullName + '\project.json'
$proj = [xml] (Get-Content $CsProjFilePath)
#Remove all references to ..packages files
$xpath = "//msb:Reference/msb:HintPath[contains(.,'..\packages')]"
$nodes = @(Select-Xml -xpath $xpath $proj -Namespace $MsbNS | foreach {$_.Node})
if (!$nodes) { Write-Verbose "RemoveElement: XPath $XPath not found" }
Write-Output 'Reference Nodes found: ' $nodes.Count
foreach($node in $nodes) {
$referenceNode = $node.ParentNode
$itemGroupNode = $referenceNode.ParentNode
[void]$itemGroupNode.RemoveChild($referenceNode)
}
[System.XML.XMLElement] $itemGroupNoneNode = $null
#Find itemgroup with None Elements, if not found add.
$itemGroupNoneNodes = @(Select-Xml -xpath "//msb:ItemGroup/msb:None" $proj -Namespace $MsbNS | foreach {$_.Node})
Write-Output '$itemGroupNoneNode found: ' $itemGroupNoneNodes.Count
if($itemGroupNoneNodes.Count -eq 0){
# create itemgroup element for None nodes.
Write-Output 'Adding ItemGroup for None Nodes'
$itemGroupNoneNode = $proj.CreateElement('ItemGroup',$proj.DocumentElement.NamespaceURI)
$itemGroupNodes = @(Select-Xml -xpath "//msb:ItemGroup" $proj -Namespace $MsbNS | foreach {$_.Node})
$itemGroupNodes.Count
[void]$proj.DocumentElement.InsertAfter($itemGroupNoneNode,$itemGroupNodes[$itemGroupNodes.Count-1])
}else{
$itemGroupNoneNode = $itemGroupNoneNodes[0].ParentNode
}
#Remove packages.config from ItemGroup
$nodes = @(Select-Xml -xpath "//msb:ItemGroup/msb:None[@Include='packages.config']" $proj -Namespace $MsbNS | foreach {$_.Node})
Write-Output 'packages.config Nodes found: ' $nodes.Count
foreach($node in $nodes) {
$itemGroupNode = $node.ParentNode
[void]$itemGroupNode.RemoveChild($node)
}
#Remove build target EnsureNuGetPackageBuildImports from csproj
$nodes = @(Select-Xml -xpath "//msb:Target[@Name='EnsureNuGetPackageBuildImports']" $proj -Namespace $MsbNS | foreach {$_.Node})
Write-Output 'EnsureNuGetPackageBuildImports target found: ' $nodes.CountAd
foreach($node in $nodes) {
$itemGroupNode = $node.ParentNode
[void]$itemGroupNode.RemoveChild($node)
}
#Add project.json to itemGroup
if( Test-Path $ProjectFilePath){
$nodes = @(Select-Xml -xpath "//msb:ItemGroup/msb:None[@Include='project.json']" $proj -Namespace $MsbNS | foreach {$_.Node})
if($nodes.Count -eq 0){
$projectJsonNoneNode = $proj.CreateElement("None", $proj.DocumentElement.NamespaceURI)
$projectJsonNoneNode.SetAttribute("Include","project.json")
[void]$itemGroupNoneNode.AppendChild($projectJsonNoneNode)
Write-Output 'Adding None node for project.json'
}
}
#add PropertyGroup nodes targetFrameworkProfile, CopyNuGetImplementations, PlatformTarget
# Find the TargetFrameworkVersion to be used to find the parent PropertyGroup node
$xpath = "//msb:PropertyGroup/msb:TargetFrameworkVersion"
$nodes = @(Select-Xml -xpath $xpath $proj -Namespace $MsbNS | foreach {$_.Node})
if ($nodes.Count -gt 0) {
[System.XML.XMLElement] $node = $nodes[0]
$propertyGroupNode = $node.ParentNode
$nodes = @(Select-Xml -xpath "//msb:PropertyGroup/msb:TargetFrameworkProfile" $proj -Namespace $MsbNS | foreach {$_.Node})
if($nodes.Count -eq 0){
$node = $proj.CreateElement("TargetFrameworkProfile", $proj.DocumentElement.NamespaceURI)
[void]$propertyGroupNode.AppendChild($node)
Write-Output 'Adding TargetFrameworkProfile node for PropertyGroup'
}
#$nodes = @(Select-Xml -xpath "//msb:PropertyGroup/msb:CopyNuGetImplementations" $proj -Namespace $MsbNS | foreach {$_.Node})
#if($nodes.Count -eq 0){
# $node = $proj.CreateElement("CopyNuGetImplementations", $proj.DocumentElement.NamespaceURI)
# $textnode = $proj.CreateTextNode("true")
# $node.AppendChild($textnode)
# [void]$propertyGroupNode.AppendChild($node)
# Write-Output 'Adding CopyNuGetImplementations node for PropertyGroup'
#}
$nodes = @(Select-Xml -xpath "//msb:PropertyGroup/msb:PlatformTarget[not(@*)]" $proj -Namespace $MsbNS | foreach {$_.Node})
if($nodes.Count -eq 0){
$node = $proj.CreateElement("PlatformTarget", $proj.DocumentElement.NamespaceURI)
$textnode = $proj.CreateTextNode("AnyCPU")
$node.AppendChild($textnode)
[void]$propertyGroupNode.AppendChild($node)
Write-Output 'Adding PlatformTarget node for PropertyGroup'
}
}
# replace ToolsVersion with 14.0
$attibutes = Select-Xml -xpath "//@ToolsVersion" $proj -Namespace $MsbNS
foreach ($attribute in $attibutes){
$attribute.Node.value = "14.0"
Write-Output 'Setting ToolsVersion to 14.0'
}
$proj.Save($CsProjFilePath)
}