2016-03-07 20:24:36 +00:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System ;
2017-04-25 19:26:21 +00:00
using System.IO ;
using System.Linq ;
using System.Xml.Linq ;
2016-03-07 20:24:36 +00:00
namespace Microsoft.DotNet.Scripts
{
/// <summary>
/// Holds the configuration information for the update-dependencies script.
/// </summary>
/// <remarks>
/// The following Environment Variables are required by this script:
2017-02-20 22:20:27 +00:00
///
2016-03-07 20:24:36 +00:00
/// GITHUB_USER - The user to commit the changes as.
/// GITHUB_EMAIL - The user's email to commit the changes as.
/// GITHUB_PASSWORD - The password/personal access token of the GitHub user.
///
/// The following Environment Variables can optionally be specified:
2017-02-20 22:20:27 +00:00
///
2017-08-14 00:48:27 +00:00
/// DOTNET_VERSION_URL - The Url to the root of the version information (this is combined with the fragments below) (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info")
2017-02-20 22:20:27 +00:00
/// ROSLYN_VERSION_FRAGMENT - The fragment to combine with DOTNET_VERSION_URL to get the current dotnet/roslyn package versions. (ex. "dotnet/roslyn/netcore1.0")
/// CORESETUP_VERSION_FRAGMENT - The fragment to combine with DOTNET_VERSION_URL to get the current dotnet/core-setup package versions. (ex. "dotnet/core-setup/master")
2016-03-07 20:24:36 +00:00
/// GITHUB_ORIGIN_OWNER - The owner of the GitHub fork to push the commit and create the PR from. (ex. "dotnet-bot")
/// GITHUB_UPSTREAM_OWNER - The owner of the GitHub base repo to create the PR to. (ex. "dotnet")
/// GITHUB_PROJECT - The repo name under the ORIGIN and UPSTREAM owners. (ex. "cli")
2017-02-20 22:20:27 +00:00
/// GITHUB_UPSTREAM_BRANCH - The branch in the GitHub base repo to create the PR to. (ex. "master");
2016-04-04 23:38:49 +00:00
/// GITHUB_PULL_REQUEST_NOTIFICATIONS - A semi-colon ';' separated list of GitHub users to notify on the PR.
2016-03-07 20:24:36 +00:00
/// </remarks>
public class Config
{
2016-04-11 18:51:35 +00:00
public static Config Instance { get ; } = new Config ( ) ;
2016-03-07 20:24:36 +00:00
2016-04-11 18:51:35 +00:00
private Lazy < string > _userName = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "GITHUB_USER" ) ) ;
private Lazy < string > _email = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "GITHUB_EMAIL" ) ) ;
private Lazy < string > _password = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "GITHUB_PASSWORD" ) ) ;
2017-02-20 22:20:27 +00:00
private Lazy < string > _dotNetVersionUrl = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "DOTNET_VERSION_URL" , "https://raw.githubusercontent.com/dotnet/versions/master/build-info" ) ) ;
2017-04-25 19:26:21 +00:00
private Lazy < string > _coreSetupVersionFragment = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "CORESETUP_VERSION_FRAGMENT" , GetDefaultCoreSetupVersionFragment ( ) ) ) ;
2018-01-30 23:43:41 +00:00
private Lazy < string > _roslynVersionFragment = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "ROSLYN_VERSION_FRAGMENT" ) ) ;
2016-04-11 18:51:35 +00:00
private Lazy < string > _gitHubUpstreamOwner = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "GITHUB_UPSTREAM_OWNER" , "dotnet" ) ) ;
private Lazy < string > _gitHubProject = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "GITHUB_PROJECT" , "cli" ) ) ;
2017-04-25 19:26:21 +00:00
private Lazy < string > _gitHubUpstreamBranch = new Lazy < string > ( ( ) = > GetEnvironmentVariable ( "GITHUB_UPSTREAM_BRANCH" , GetDefaultUpstreamBranch ( ) ) ) ;
2017-02-20 22:20:27 +00:00
private Lazy < string [ ] > _gitHubPullRequestNotifications = new Lazy < string [ ] > ( ( ) = >
2016-04-11 18:51:35 +00:00
GetEnvironmentVariable ( "GITHUB_PULL_REQUEST_NOTIFICATIONS" , "" )
. Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) ) ;
2018-01-30 23:43:41 +00:00
2016-04-11 18:51:35 +00:00
private Config ( )
2016-03-07 20:24:36 +00:00
{
}
2016-04-11 18:51:35 +00:00
public string UserName = > _userName . Value ;
2017-02-20 22:20:27 +00:00
public string Email = > _email . Value ;
2016-04-11 18:51:35 +00:00
public string Password = > _password . Value ;
2017-02-20 22:20:27 +00:00
public string DotNetVersionUrl = > _dotNetVersionUrl . Value ;
public string CoreSetupVersionFragment = > _coreSetupVersionFragment . Value ;
2018-01-30 23:43:41 +00:00
public string RoslynVersionFragment = > _roslynVersionFragment . Value ;
public bool HasRoslynVersionFragment = > ! string . IsNullOrEmpty ( RoslynVersionFragment ) ;
2016-04-11 18:51:35 +00:00
public string GitHubUpstreamOwner = > _gitHubUpstreamOwner . Value ;
public string GitHubProject = > _gitHubProject . Value ;
public string GitHubUpstreamBranch = > _gitHubUpstreamBranch . Value ;
public string [ ] GitHubPullRequestNotifications = > _gitHubPullRequestNotifications . Value ;
2016-03-07 20:24:36 +00:00
private static string GetEnvironmentVariable ( string name , string defaultValue = null )
{
string value = Environment . GetEnvironmentVariable ( name ) ;
2016-04-04 23:38:49 +00:00
if ( value = = null )
2016-03-07 20:24:36 +00:00
{
value = defaultValue ;
}
2016-04-04 23:38:49 +00:00
if ( value = = null )
2016-03-07 20:24:36 +00:00
{
2016-07-16 00:09:38 +00:00
throw new InvalidOperationException ( $"Can't find environment variable '{name}'." ) ;
2016-03-07 20:24:36 +00:00
}
return value ;
}
2017-04-25 19:26:21 +00:00
private static string GetDefaultUpstreamBranch ( )
{
return GetRepoMSBuildPropValue ( "BranchInfo.props" , "BranchName" ) ? ? "master" ;
}
private static string GetDefaultCoreSetupVersionFragment ( )
{
2017-06-23 13:53:04 +00:00
// by default, the current core-setup branch should match the current cli branch name
string coreSetupChannel = Instance . GitHubUpstreamBranch ;
2017-04-25 19:26:21 +00:00
return $"dotnet/core-setup/{coreSetupChannel}" ;
}
private static string GetRepoMSBuildPropValue ( string propsFileName , string propertyName )
{
var propsFilePath = Path . Combine ( Dirs . RepoRoot , "build" , propsFileName ) ;
var root = XDocument . Load ( propsFilePath ) . Root ;
var ns = root . Name . Namespace ;
var value = root
. Elements ( ns + "PropertyGroup" )
. Elements ( ns + propertyName )
. FirstOrDefault ( )
? . Value ;
if ( string . IsNullOrEmpty ( value ) )
{
Console . WriteLine ( $"Could not find a property named '{propertyName}' in {propsFilePath}" ) ;
return null ;
}
return value ;
}
2016-03-07 20:24:36 +00:00
}
}