Updated the build.

This commit is contained in:
David Fowler 2014-01-25 02:12:04 -08:00
parent a0de7048b5
commit 75169fefff
13 changed files with 8 additions and 579 deletions

1
.gitignore vendored
View File

@ -26,3 +26,4 @@ PublishProfiles/
*.vspx
/.symbols
nuget.exe
build/

View File

@ -1,11 +1,16 @@
@echo off
cd %~dp0
IF EXIST .nuget\NuGet.exe goto part2
IF EXIST .nuget\NuGet.exe goto restore
echo Downloading latest version of NuGet.exe...
md .nuget
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest 'https://www.nuget.org/nuget.exe' -OutFile '.nuget\NuGet.exe'"
:part2
:restore
IF EXIST build goto run
.nuget\NuGet.exe install KoreBuild -ExcludeVersion -o packages
xcopy packages\KoreBuild\build build\ /Y
.nuget\NuGet.exe install Sake -version 0.2 -o packages
:run
packages\Sake.0.2\tools\Sake.exe -I build -f makefile.shade %*

View File

@ -1,9 +0,0 @@
default gitBranch=''
var gitCommand='clone ${gitUri}'
set gitCommand='${gitCommand} --branch ${gitBranch}' if='!string.IsNullOrEmpty(gitBranch)'
git

View File

@ -1,8 +0,0 @@
default gitBranch=''
var gitCommand='pull ${gitUri}'
set gitCommand='${gitCommand} ${gitBranch}' if='!string.IsNullOrEmpty(gitBranch)'
git

View File

@ -1,5 +0,0 @@
default gitFolder=''
exec program='git' commandline='${gitCommand}' workingdir='${gitFolder}'

View File

@ -1,17 +0,0 @@
@{/*
k-build
Builds project. Downloads and executes k sdk tools.
projectFile=''
Required. Path to the project.json to build.
*/}
var projectFolder='${Path.GetDirectoryName(projectFile)}'
var projectName='${Path.GetFileName(projectFolder)}'
var projectBin='${Path.Combine(projectFolder, "bin")}'
directory delete="${projectBin}"
k command='build ${projectFolder}'
copy sourceDir='${projectBin}' outputDir='${Path.Combine(BUILD_DIR, projectName)}'

View File

@ -1,13 +0,0 @@
@{/*
k-clean
Cleans project. Downloads and executes k sdk tools.
projectFile=''
Required. Path to the project.json to build.
*/}
var projectFolder='${Path.GetDirectoryName(projectFile)}'
k command='clean ${projectFolder}'

View File

@ -1,330 +0,0 @@
use namespace="System"
use namespace="System.Collections.Generic"
use namespace="System.IO"
use namespace="System.Linq"
use namespace="System.Reflection"
use namespace="System.Text"
use namespace="System.Web.Script.Serialization"
use namespace="System.Xml.Linq"
use assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
@{/*
k-generate-projects
Generate csproj files from project.json
solutionPath=''
Required. Path to the solution directory
*/}
content var='net45' include href='net45.txt'
content var='k10' include href='k10.txt'
@{
ProjectGenerator.Logger = Log;
var templates = new Dictionary<string, string> {
{ "net45", net45 },
{ "k10", k10 }
};
ProjectGenerator.MakeProjects(solutionPath, templates);
}
functions
@{
class ProjectGenerator
{
public static Sake.Engine.Logging.ILog Logger { get; set; }
static void Log(string message, params object[] args)
{
Logger.Info(String.Format(message, args));
}
static void Warn(string message, params object[] args)
{
Logger.Warn(String.Format(message, args));
}
public static void MakeProjects(string solutionPath, IDictionary<string, string> templates)
{
var jsonFiles = GetJsonFiles(solutionPath);
var projectMapping = GetProjectMapping(solutionPath, jsonFiles);
Log("Found {0} projects", jsonFiles.Length);
foreach (var p in jsonFiles)
{
Log(p);
}
foreach (var path in jsonFiles)
{
ProduceProjectFilesForProject(path, projectMapping, templates);
}
}
private static string[] GetJsonFiles(string solutionPath)
{
Func<string, string[]> getFiles = dir =>
{
string path = Path.Combine(solutionPath, dir);
if (!Directory.Exists(path))
{
return new string[0];
}
return Directory.GetFiles(path, "project.json", SearchOption.AllDirectories);
};
return getFiles("src").Concat(getFiles("samples"))
.Concat(getFiles("test"))
.ToArray();
}
private static IDictionary<string, object> GetProjectMapping(string solutionPath, string[] jsonFiles)
{
var dict = new Dictionary<string, object>();
foreach (var path in jsonFiles)
{
string projectDir = Path.GetDirectoryName(path);
string projectName = projectDir.Substring(Path.GetDirectoryName(projectDir).Length).Trim(Path.DirectorySeparatorChar);
// {
// "p1" : { "net45" : "id", "k10" : "pid1", path: "src\p1" },
// "p2" : { "net45" : "id", "k10" : "pid2", path: "src\p2" }
// }
//
string net45Project = Path.Combine(projectDir, GetProjectFileName(projectName, "net45"));
string k10Project = Path.Combine(projectDir, GetProjectFileName(projectName, "k10"));
var configs = new Dictionary<string, object>();
configs["net45"] = GetProjectGuidFromFileOrCreateNew(net45Project);
configs["k10"] = GetProjectGuidFromFileOrCreateNew(k10Project);
configs["path"] = Path.GetDirectoryName(path.Substring(solutionPath.Length).TrimStart(Path.DirectorySeparatorChar));
dict[projectName] = configs;
}
return dict;
}
private static string GetProjectGuidFromFileOrCreateNew(string projectPath)
{
if (!File.Exists(projectPath))
{
return Guid.NewGuid().ToString().ToUpper();
}
var projectGuid = XDocument.Parse(File.ReadAllText(projectPath))
.Descendants()
.FirstOrDefault(e => e.Name.LocalName.Equals("ProjectGuid"));
if (projectGuid == null)
{
return Guid.NewGuid().ToString();
}
return projectGuid.Value.Trim((char)'{', (char)'}');
}
private static void ProduceProjectFilesForProject(string jsonPath,
IDictionary<string, object> projectMapping,
IDictionary<string, string> templates)
{
var serializer = new JavaScriptSerializer();
string projectDir = Path.GetDirectoryName(jsonPath);
string projectName = projectDir.Substring(Path.GetDirectoryName(projectDir).Length).Trim(Path.DirectorySeparatorChar);
Log("Generated projects for {0}", projectName);
var jsonText = File.ReadAllText(jsonPath);
var d = serializer.DeserializeObject(jsonText) as IDictionary<string, object>;
var configs = GetObject(d, "configurations");
var references = GetObject(d, "dependencies") ?? new Dictionary<string, object>();
// Get the list of files
var filesString = String.Join(Environment.NewLine,
Directory.GetFiles(projectDir, "*.cs", SearchOption.AllDirectories)
.Select(p => p.Substring(projectDir.Length).Trim(Path.DirectorySeparatorChar))
.Where(p => !p.StartsWith("obj"))
.Select(p => String.Format(
@"<Compile Include=""{0}"" />", p)));
// Add the config file if it's there
if (File.Exists(Path.Combine(projectDir, "packages.config")))
{
filesString += "<Content Include=\"packages.config\" />";
}
var packageReferences = references.Where(r => !String.IsNullOrEmpty((string)r.Value))
.ToDictionary(k => k.Key, k => (string)k.Value);
var projectReferences = references.Where(r => String.IsNullOrEmpty((string)r.Value))
.Select(r => r.Key)
.ToArray();
// HACK: Assume the packages folder is 2 up from the projectDir
string packagesDir = Path.GetFullPath(Path.Combine(projectDir, "..", "..", "packages"));
foreach (var targetFramework in configs.Keys)
{
string id = (string)GetObject(projectMapping, projectName)[targetFramework];
var template = templates[targetFramework]
.Replace("{ProjectGuid}", id)
.Replace("{Name}", projectName)
.Replace("{Files}", filesString)
.Replace("{ProjectReferences}", BuildProjectReferences(projectReferences, targetFramework, projectMapping))
.Replace("{References}", BuildReferences(packageReferences, packagesDir, targetFramework, GetCandidates(targetFramework)));
if (targetFramework.StartsWith("k"))
{
template = template.Replace("{CSharpTargetsPath}", GetProjectKTargets(packagesDir));
}
string output = Path.Combine(projectDir, GetProjectFileName(projectName, targetFramework));
Log("Generated {0}", output);
File.WriteAllText(output, template);
}
}
private static string GetProjectKTargets(string packagesDir)
{
var projectK = Directory.GetDirectories(packagesDir, "ProjectK*")
.Select(p => new { Path = p, Build = Int32.Parse(p.Substring(p.LastIndexOf('-') + 1)) })
.OrderByDescending(p => p.Build)
.FirstOrDefault();
if (projectK == null)
{
Warn("Project K targets aren't installed");
return "";
}
return Path.Combine("..", "..", projectK.Path.Substring(projectK.Path.IndexOf("packages")), "Framework\\K\\v1.0\\ProjectK.CSharp.targets");
}
private static string GetProjectFileName(string projectName, string config)
{
return projectName + "." + config + ".csproj";
}
private static string BuildProjectReferences(string[] projectReferences, string config, IDictionary<string, object> projectMapping)
{
if (projectReferences.Length == 0)
{
return "";
}
var sb = new StringBuilder();
foreach (var reference in projectReferences)
{
var info = GetObject(projectMapping, reference);
if (info == null)
{
Warn("No project reference found for {0}", reference);
continue;
}
string projectFileName = GetProjectFileName(reference, config);
string path = Path.Combine((string)info["path"], projectFileName);
sb.AppendFormat(@"<ProjectReference Include=""..\..\{0}"">
<Project>{{{1}}}</Project>
<Name>{2}</Name>
</ProjectReference>", path, info[config], reference);
}
return sb.ToString();
}
private static string[] GetCandidates(string config)
{
if (config == "net45")
{
return new[] { "net45", "net40", "net35", "net20" };
}
return new[] { config };
}
private static string BuildReferences(IDictionary<string, string> references, string packagesDir, string configName, string[] candidates)
{
if (references.Count == 0)
{
return "";
}
Log("Building package references for {0}", configName);
var sb = new StringBuilder();
foreach (var reference in references)
{
var version = (string)reference.Value;
string pattern = version.IndexOf("*") != -1 ? reference.Key + "*" : reference.Key + "." + reference.Value;
var packageDir = Directory.GetDirectories(packagesDir, pattern).FirstOrDefault();
if (packageDir == null)
{
Warn(reference.Key + " = " + version + " ==> UNRESOLVED");
continue;
}
Log(reference.Key + " = " + version + " ==> " + packageDir);
var candidate = candidates.Select(c => Path.Combine(packageDir, "lib", c))
.FirstOrDefault(Directory.Exists);
if (candidate == null)
{
Warn("Unable to find package reference for {0}, target framework = {1}", reference.Key, configName);
continue;
}
var dlls = Directory.EnumerateFiles(candidate, "*.dll")
.Distinct()
.Where(File.Exists)
.ToList();
foreach (var dllPath in dlls)
{
sb.AppendFormat(@"
<Reference Include=""{0}"">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\{1}</HintPath>
</Reference>", AssemblyName.GetAssemblyName(dllPath).FullName, dllPath.Substring(dllPath.IndexOf("packages")));
}
}
return sb.ToString();
}
private static IDictionary<string, object> GetObject(IDictionary<string, object> obj, string key)
{
object value;
if (obj.TryGetValue(key, out value))
{
return value as IDictionary<string, object>;
}
return null;
}
}
}

View File

@ -1,7 +0,0 @@
@{/*
k-restore
Restores nuget packages required for k projects. Downloads and executes k sdk tools.
*/}
k command='restore'

View File

@ -1,47 +0,0 @@
use namespace="System"
use namespace="System.IO"
use import="Files"
default BASE_DIR='${Directory.GetCurrentDirectory()}'
default TARGET_DIR='${Path.Combine(BASE_DIR, "artifacts")}'
default BUILD_DIR='${Path.Combine(TARGET_DIR, "build")}'
default TEST_DIR='${Path.Combine(TARGET_DIR, "test")}'
@{
E("K_BUILD_VERSION", "t" + DateTime.UtcNow.ToString("yyMMddHHmmss"));
}
#target-default target='default'
k-restore
k-generate-projects solutionPath='${BASE_DIR}'
#target-dir-clean target='clean'
directory delete="${TARGET_DIR}"
#build-clean target='clean'
k-clean each='var projectFile in Files.Include("src/**/project.json")'
#build-compile target='compile'
k-build each='var projectFile in Files.Include("src/**/project.json")'
@{
foreach (var nupkg in Files.Include(Path.Combine(BUILD_DIR, "*/*.nupkg")))
{
File.Copy(nupkg, Path.Combine(BUILD_DIR, Path.GetFileName(nupkg)), true);
}
}
#nuget-install target='install' description='Copy NuGet packages to local repo'
@{
var HOME_DIR = E("HOME");
if (string.IsNullOrEmpty(HOME_DIR))
{
HOME_DIR = E("HOMEDRIVE") + E("HOMEPATH");
}
}
copy sourceDir='${BUILD_DIR}' include='*.nupkg' outputDir='${Path.Combine(HOME_DIR, ".nuget")}' overwrite='${true}'
functions @{
string E(string key) { return Environment.GetEnvironmentVariable(key); }
void E(string key, string value) { Environment.SetEnvironmentVariable(key, value); }
}

View File

@ -1,31 +0,0 @@
@{/*
k
Run klr commands in your project. Downloads and executes k sdk.
kVersion='0.0.1-pre-30109-087'
May be passed to override the nuget package version holding xunit console runner.
kProgram='...'
May be passed to override the path to the xunit program that will be executed
command=''
*/}
default kLatestSuccessful='\\wsr-teamcity\Drops\ProjectK.Main\latest-successful\sdk'
default kVersion=''
test if='string.IsNullOrEmpty(kVersion)'
for each='var file in System.IO.Directory.EnumerateFiles(kLatestSuccessful).Select(System.IO.Path.GetFileName)'
test if='file.StartsWith("ProjectK.") && file.EndsWith(".nupkg")'
- kVersion = file.Substring("ProjectK.".Length, file.Length - "ProjectK.".Length - ".nupkg".Length);
default kProgram='packages/ProjectK.${kVersion}/tools/k.cmd'
-// Download xunit from nuget sources if not already present
test if='!File.Exists(kProgram)'
log info='Installing ProjectK ${kVersion} from ${kLatestSuccessful}'
nuget-install package='ProjectK' packageVersion='${kVersion}' outputDir='packages' extra='-Source ${kLatestSuccessful}'
exec program='${Path.GetFullPath(kProgram)}' commandline='${command}'

View File

@ -1,53 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{{ProjectGuid}}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>{Name}</RootNamespace>
<AssemblyName>{Name}</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<BaseIntermediateOutputPath>obj/K</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\K</OutputPath>
<DefineConstants>DEBUG;TRACE;K10</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\K</OutputPath>
<DefineConstants>TRACE;K10</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
{Files}
<Content Include="project.json" />
</ItemGroup>
<ItemGroup>
{References}
</ItemGroup>
<ItemGroup>
{ProjectReferences}
</ItemGroup>
<Import Project="{CSharpTargetsPath}" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{{ProjectGuid}}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>{Name}</RootNamespace>
<AssemblyName>{Name}</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<BaseIntermediateOutputPath>obj/net45</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\net45</OutputPath>
<DefineConstants>DEBUG;TRACE;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\net45</OutputPath>
<DefineConstants>TRACE;NET45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
{References}
</ItemGroup>
<ItemGroup>
{Files}
<Content Include="project.json" />
</ItemGroup>
<ItemGroup>
{ProjectReferences}
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>