Skip to content

Commit

Permalink
Upgrade buildalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
georgewfraser committed Apr 29, 2019
1 parent f7b5dcd commit 60dd338
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
62 changes: 24 additions & 38 deletions src/ProjectCracker/ProjectCracker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -322,41 +322,23 @@ let private parseProjectAssets(projectAssetsJson: FileInfo): ProjectAssets =
let private project(fsproj: FileInfo): ProjectAnalyzer =
let options = new AnalyzerManagerOptions()
options.LogWriter <- !diagnosticsLog // TODO this doesn't follow ref changes
options.CleanBeforeCompile <- false
let manager = AnalyzerManager(options)
manager.GetProject(fsproj.FullName)

let private inferTargetFramework(fsproj: FileInfo): ProjectAnalyzer =
let analyzer = project(fsproj)
let props = analyzer.Project.Properties
let findTargetFramework =
[ for p in props do
if p.Name = "TargetFramework" then
yield p ]
if not(List.isEmpty findTargetFramework) then
analyzer
else
// TODO get this from project.assets.json
dprintfn "No TargetFramework in %s, looking for TargetFrameworks" fsproj.Name
let targetFrameworks =
[ for p in props do
if p.Name = "TargetFrameworks" then
yield! p.EvaluatedValue.Split(';') ]
let preferenceOrder =
[ for shortFramework, _ in frameworkPreference do
if List.contains shortFramework targetFrameworks then
yield shortFramework ]
let targetFramework =
if List.isEmpty preferenceOrder then
dprintfn "Couldn't find TargetFrameworks in %s, defaulting to netcoreapp2.1" fsproj.Name
"netcoreapp2.1"
else
let chosen = List.head preferenceOrder
dprintfn "Chose TargetFramework %s from %A" chosen preferenceOrder
chosen
let analyzer = project(fsproj)
analyzer.SetGlobalProperty("TargetFramework", targetFramework)
analyzer
let private inferTargetFramework(fsproj: FileInfo): AnalyzerResult =
let builds = project(fsproj).Build()
// TODO get target framework from project.assets.json
let mutable chosen: AnalyzerResult option = None
for shortFramework, _ in frameworkPreference do
if chosen.IsNone then
for build in builds do
if build.TargetFramework = shortFramework then
chosen <- Some(build)
if chosen.IsNone then
for build in builds do
if chosen.IsNone then
chosen <- Some(build)
chosen.Value

let private projectTarget(csproj: FileInfo) =
let baseName = Path.GetFileNameWithoutExtension(csproj.Name)
Expand All @@ -372,7 +354,7 @@ let private projectTarget(csproj: FileInfo) =
placeholderTarget

let private absoluteIncludePath(fsproj: FileInfo, i: ProjectItem) =
let relativePath = i.EvaluatedInclude.Replace('\\', Path.DirectorySeparatorChar)
let relativePath = i.ItemSpec.Replace('\\', Path.DirectorySeparatorChar)
let absolutePath = Path.Combine(fsproj.DirectoryName, relativePath)
let normalizePath = Path.GetFullPath(absolutePath)
FileInfo(normalizePath)
Expand All @@ -389,13 +371,17 @@ let crack(fsproj: FileInfo): CrackedProject =
try
// Get source info from .fsproj
let timeProject = Stopwatch.StartNew()
let project = inferTargetFramework(fsproj).Load()
let project = inferTargetFramework(fsproj)
let sources =
[ for i in project.GetItems("Compile") do
yield absoluteIncludePath(fsproj, i) ]
[ for KeyValue(k, v) in project.Items do
if k = "Compile" then
for i in v do
yield absoluteIncludePath(fsproj, i) ]
let directReferences =
[ for i in project.GetItems("Reference") do
yield absoluteIncludePath(fsproj, i) ]
[ for KeyValue(k, v) in project.Items do
if k = "Reference" then
for i in v do
yield absoluteIncludePath(fsproj, i) ]
dprintfn "Cracked %s in %dms" fsproj.Name timeProject.ElapsedMilliseconds
// Get package info from project.assets.json
let projectAssetsJson = FileInfo(Path.Combine [|fsproj.DirectoryName; "obj"; "project.assets.json"|])
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectCracker/ProjectCracker.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Buildalyzer" Version="0.4.0" />
<PackageReference Include="Buildalyzer" Version="2.2.0" />
<!-- Fix warnings from Buildalyzer indirect refs to old versions -->
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
Expand Down
10 changes: 7 additions & 3 deletions tests/ProjectCracker.Tests/ProjectCrackerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ let ``issue 28``() =

[<Test>]
let ``error for unbuilt project``() =
let bin = Path.Combine [|projectRoot.FullName; "sample"; "NotBuilt"; "bin"|]
let obj = Path.Combine [|projectRoot.FullName; "sample"; "NotBuilt"; "obj"|]
Directory.Delete(bin, true)
Directory.Delete(obj, true)
let fsproj = Path.Combine [|projectRoot.FullName; "sample"; "NotBuilt"; "NotBuilt.fsproj"|] |> FileInfo
let cracked = ProjectCracker.crack(fsproj)
match cracked.error with
| None -> Assert.Fail("Should have failed to crack unbuilt project")
| Some(e) -> StringAssert.Contains("project.assets.json does not exist", e)
if cracked.error.IsSome then Assert.Fail(cracked.error.Value)
CollectionAssert.AreEquivalent(["NotBuilt.fs"], [for f in cracked.sources do yield f.Name])
CollectionAssert.IsNotEmpty(cracked.packageReferences)

0 comments on commit 60dd338

Please sign in to comment.