Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Ignore global.json files when emitting projects during publish to fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo committed Sep 16, 2015
1 parent b8fc4b2 commit 021115a
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 10 deletions.
3 changes: 3 additions & 0 deletions misc/GlobalJsonInProjectDir/Project/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"userkey": "uservalue"
}
7 changes: 7 additions & 0 deletions misc/GlobalJsonInProjectDir/Project/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {},
"frameworks": {
"dnx451": {},
"dnxcore50": {}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.Dnx.Runtime/ProjectFilesCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.Dnx.Runtime
public class ProjectFilesCollection
{
public static readonly string[] DefaultCompileBuiltInPatterns = new[] { @"**/*.cs" };
public static readonly string[] DefaultPublishExcludePatterns = new[] { @"obj/**/*.*", @"bin/**/*.*", @"**/.*/**" };
public static readonly string[] DefaultPublishExcludePatterns = new[] { @"obj/**/*.*", @"bin/**/*.*", @"**/.*/**", @"**/global.json" };
public static readonly string[] DefaultPreprocessPatterns = new[] { @"compiler/preprocess/**/*.cs" };
public static readonly string[] DefaultSharedPatterns = new[] { @"compiler/shared/**/*.cs" };
public static readonly string[] DefaultResourcesBuiltInPatterns = new[] { @"compiler/resources/**/*", "**/*.resx" };
Expand Down
7 changes: 4 additions & 3 deletions test/Microsoft.Dnx.Testing.CommonUtils/ProjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@

using System;
using System.IO;
using Microsoft.Dnx.Runtime;
using Newtonsoft.Json.Linq;

namespace Microsoft.Dnx.Testing
{
public static class ProjectExtensions
{
public static string GetBinPath(this Runtime.Project project)
public static string GetBinPath(this Project project)
{
return Path.Combine(project.ProjectDirectory, "bin");
}

public static string GetLocalPackagesDir(this Runtime.Project project)
public static string GetLocalPackagesDir(this Project project)
{
return Path.Combine(project.ProjectDirectory, "packages");
}

public static void UpdateProjectFile(this Runtime.Project project, Action<JObject> updateContents)
public static void UpdateProjectFile(this Project project, Action<JObject> updateContents)
{
JsonUtils.UpdateJson(project.ProjectFilePath, updateContents);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.


using System.Collections.Generic;

namespace Microsoft.Dnx.Testing
{
internal interface IProjectResolver
{
IEnumerable<string> SearchPaths { get; }

bool TryResolveProject(string name, out Runtime.Project project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Dnx.Runtime;

namespace Microsoft.Dnx.Testing
{
internal class ProjectResolver : IProjectResolver
{
private readonly HashSet<string> _searchPaths = new HashSet<string>();
private ILookup<string, ProjectInformation> _projects;

public ProjectResolver(string projectPath)
{
var rootPath = ResolveRootDirectory(projectPath);
Initialize(projectPath, rootPath);
}

public ProjectResolver(string projectPath, string rootPath)
{
Initialize(projectPath, rootPath);
}

public IEnumerable<string> SearchPaths
{
get
{
return _searchPaths;
}
}

public bool TryResolveProject(string name, out Runtime.Project project)
{
project = null;

if (!_projects.Contains(name))
{
return false;
}

// ProjectInformation.Project is lazily evaluated only once and
// it returns null when ProjectInformation.FullPath doesn't contain project.json
var candidates = _projects[name].Where(p => p.Project != null);
if (candidates.Count() > 1)
{
var allCandidatePaths = string.Join(Environment.NewLine, candidates.Select(x => x.FullPath).OrderBy(x => x));
throw new InvalidOperationException(
$"The project name '{name}' is ambiguous between the following projects:{Environment.NewLine}{allCandidatePaths}");
}

project = candidates.SingleOrDefault()?.Project;
return project != null;
}

private void Initialize(string projectPath, string rootPath)
{
_searchPaths.Add(new DirectoryInfo(projectPath).Parent.FullName);

GlobalSettings global;

if (GlobalSettings.TryGetGlobalSettings(rootPath, out global))
{
foreach (var sourcePath in global.ProjectSearchPaths)
{
_searchPaths.Add(Path.Combine(rootPath, sourcePath));
}
}

Func<DirectoryInfo, ProjectInformation> dirInfoToProjectInfo = d => new ProjectInformation
{
// The name of the folder is the project
Name = d.Name,
FullPath = d.FullName
};

// Resolve all of the potential projects
_projects = _searchPaths.Select(path => new DirectoryInfo(path))
.Where(d => d.Exists)
.SelectMany(d => new[] { d }.Concat(d.EnumerateDirectories()))
.Distinct(new DirectoryInfoFullPathComparator())
.Select(dirInfoToProjectInfo)
.ToLookup(d => d.Name);
}

public void Clear()
{
_searchPaths.Clear();
_projects = null;
}

public static string ResolveRootDirectory(string projectPath)
{
return ProjectRootResolver.ResolveRootDirectory(projectPath);
}

private class ProjectInformation
{
private Runtime.Project _project;
private bool _initialized;
private object _lockObj = new object();

public string Name { get; set; }

public string FullPath { get; set; }

public Runtime.Project Project
{
get
{
return LazyInitializer.EnsureInitialized(ref _project, ref _initialized, ref _lockObj, () =>
{
Runtime.Project project;
Runtime.Project.TryGetProject(FullPath, out project);
return project;
});
}
}
}

private class DirectoryInfoFullPathComparator : IEqualityComparer<DirectoryInfo>
{
public bool Equals(DirectoryInfo x, DirectoryInfo y)
{
return string.Equals(x.FullName, y.FullName);
}

public int GetHashCode(DirectoryInfo obj)
{
return obj.FullName.GetHashCode();
}
}
}
}
3 changes: 1 addition & 2 deletions test/Microsoft.Dnx.Testing.CommonUtils/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Linq;
using Microsoft.Dnx.Runtime;
using Microsoft.Dnx.Tooling;

namespace Microsoft.Dnx.Testing
{
Expand All @@ -28,7 +27,7 @@ public Solution(string rootPath)

public string RootPath { get; private set; }

public IEnumerable<Runtime.Project> Projects
public IEnumerable<Project> Projects
{
get
{
Expand Down
4 changes: 0 additions & 4 deletions test/Microsoft.Dnx.Testing.CommonUtils/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"Newtonsoft.Json": "6.0.6",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"compile": [
"../../src/Microsoft.Dnx.Tooling/ProjectResolver.cs",
"../../src/Microsoft.Dnx.Tooling/IProjectResolver.cs"
],
"frameworks": {
"dnx451": { },
"dnxcore50": { }
Expand Down
84 changes: 84 additions & 0 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/DnuPublishTests2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Runtime.Versioning;
using Microsoft.Dnx.Runtime;
using Microsoft.Dnx.Runtime.Helpers;
using Microsoft.Dnx.Testing;
using Microsoft.Dnx.Tooling.Publish;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Dnx.Tooling.FunctionalTests
{
[Collection(nameof(ToolingFunctionalTestCollection))]
public class DnuPublishTests2 : DnxSdkFunctionalTestBase
{
[Theory]
[MemberData(nameof(DnxSdks))]
public void GlobalJsonInProjectDir(DnxSdk sdk)
{
const string solutionName = "GlobalJsonInProjectDir";

var solution = TestUtils.GetSolution<DnuPublishTests>(sdk, solutionName);
var projectPath = Path.Combine(solution.RootPath, "Project");
var outputPath = Path.Combine(solution.RootPath, "Output");

var expectedOutputProjectJson = new JObject
{
["dependencies"] = new JObject { },
["frameworks"] = new JObject
{
["dnx451"] = new JObject { },
["dnxcore50"] = new JObject { }
}
};

var expectedOutputGlobalJson = new JObject
{
["userkey"] = "uservalue",
["projects"] = new JArray("src"),
["packages"] = "packages"
};

var expectedOutputLockFile = new JObject
{
["locked"] = false,
["version"] = Constants.LockFileVersion,
["targets"] = new JObject
{
["DNX,Version=v4.5.1"] = new JObject { },
["DNXCore,Version=v5.0"] = new JObject { },
},
["libraries"] = new JObject { },
["projectFileDependencyGroups"] = new JObject
{
[""] = new JArray(),
["DNX,Version=v4.5.1"] = new JArray(),
["DNXCore,Version=v5.0"] = new JArray()
}
};
var expectedOutputStructure = new Dir
{
["approot"] = new Dir
{
[$"src/Project"] = new Dir
{
["project.json"] = expectedOutputProjectJson,
["project.lock.json"] = expectedOutputLockFile
},
[$"global.json"] = expectedOutputGlobalJson
}
};

var result = sdk.Dnu.Publish(
projectPath,
outputPath);
result.EnsureSuccess();

var actualOutputStructure = new Dir(outputPath);
DirAssert.Equal(expectedOutputStructure, actualOutputStructure);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Xunit;

namespace Microsoft.Dnx.Tooling.FunctionalTests
{

[CollectionDefinition(nameof(ToolingFunctionalTestCollection))]
public class ToolingFunctionalTestCollection : ICollectionFixture<ToolingFunctionalTestFixture>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.Dnx.Testing;

namespace Microsoft.Dnx.Tooling.FunctionalTests
{
public class ToolingFunctionalTestFixture : IDisposable
{
public ToolingFunctionalTestFixture()
{
Console.WriteLine($@"
Environment information:
DNX_HOME: {Environment.GetEnvironmentVariable("DNX_HOME")}
DNX_SDK_VERSION_FOR_TESTING: {Environment.GetEnvironmentVariable("DNX_SDK_VERSION_FOR_TESTING")}
Information of DNX under testing:
DNX Home: {DnxSdk.GetRuntimeHome()}
DNX Version: {DnxSdkFunctionalTestBase.SdkVersionForTesting}
");
}

public void Dispose()
{

}
}
}
1 change: 1 addition & 0 deletions test/Microsoft.Dnx.Tooling.FunctionalTests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Dnx.CommonTestUtils": "1.0.0-*",
"Microsoft.Dnx.Runtime.Sources": "1.0.0-*",
"Microsoft.Dnx.Testing.CommonUtils": "1.0.0-*",
"Microsoft.Dnx.Tooling": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
Expand Down

0 comments on commit 021115a

Please sign in to comment.