Skip to content

Commit

Permalink
merge latest for release
Browse files Browse the repository at this point in the history
  • Loading branch information
bgold09 authored Dec 20, 2021
2 parents ebb44ee + 77abd94 commit 354fa9e
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
dotnet-version: 6.0.x
source-url: https://nuget.pkg.github.com/${{github.repository_owner}}/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand Down
8 changes: 8 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"line-length": {
"line_length": 104,
"code_blocks": false
},
"no-duplicate-header": false,
"ul-start-left": false
}
4 changes: 2 additions & 2 deletions Cnct/Cnct.Core.Tests/Cnct.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
89 changes: 88 additions & 1 deletion Cnct/Cnct.Core.Tests/LinkTaskSpecificationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cnct.Core.Configuration;
Expand Down Expand Up @@ -84,5 +85,91 @@ public void ExplicitLinkPath()
Assert.Single(links);
Assert.Equal(explicitLinkPath, links.Single());
}

/// <summary>
/// If Windows-specific links are specified, they should only be created in a Windows enviromment.
/// </summary>
[Fact]
public void CreateWindowsLinks()
{
TestPlatformLinks(PlatformType.Windows, new SymlinkSpecification
{
Windows = Array.Empty<string>(),
});
}

/// <summary>
/// If Linux-specific links are specified, they should only be created in a Linux environment.
/// </summary>
[Fact]
public void CreateLinuxLinks()
{
TestPlatformLinks(PlatformType.Linux, new SymlinkSpecification
{
Linux = Array.Empty<string>(),
});
}

/// <summary>
/// If OSX-specific links are specified, they should only be created in an OSX environment.
/// </summary>
[Fact]
public void CreateOsxLinks()
{
TestPlatformLinks(PlatformType.OSX, new SymlinkSpecification
{
Osx = Array.Empty<string>(),
});
}

/// <summary>
/// If UNIX-specific links are specified, they should only be created in UNIX environments (Linux and OSX).
/// </summary>
/// <param name="allowedPlatform">The platform that should create links.</param>
[Fact]
public void CreateUnixLinks()
{
TestPlatformLinks(
Platform.CurrentPlatformIsUnix,
new SymlinkSpecification
{
Unix = Array.Empty<string>(),
});
}

private static void TestPlatformLinks(bool predicate, SymlinkSpecification symlinkSpec)
{
string configRootDirectory = "test";
string target = "file.ext";
string expectedFullTargetPath = $"{configRootDirectory}{Path.DirectorySeparatorChar}{target}";
var linkSpec = new LinkTaskSpecification()
{
Links = new Dictionary<string, object>()
{
[target] = symlinkSpec,
},
};

var actualLinks = linkSpec.GetLinkConfigurations(configRootDirectory);

if (predicate)
{
Assert.Equal(1, actualLinks.Count);
Assert.Contains(expectedFullTargetPath, actualLinks);

IEnumerable<string> links = actualLinks[expectedFullTargetPath];
Assert.Single(links);
Assert.Equal($"{Platform.Home}{Path.DirectorySeparatorChar}.{target}", links.Single());
}
else
{
Assert.Empty(actualLinks);
}
}

private static void TestPlatformLinks(PlatformType allowedPlatform, SymlinkSpecification symlinkSpec)
{
TestPlatformLinks(allowedPlatform == Platform.CurrentPlatform, symlinkSpec);
}
}
}
14 changes: 14 additions & 0 deletions Cnct/Cnct.Core.Tests/PlatformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,19 @@ public void IdentifyCurrentPlatform()

Assert.Equal(expectedPlatformType, actualPlatformType);
}

[Fact]
public void IsUnixPlatform()
{
bool isUnix = Platform.CurrentPlatformIsUnix;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Assert.True(isUnix);
}
else
{
Assert.False(isUnix);
}
}
}
}
10 changes: 5 additions & 5 deletions Cnct/Cnct.Core/Cnct.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>Cnct.Core</AssemblyName>
<RootNamespace>Cnct.Core</RootNamespace>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20214.1" />
<PackageReference Include="System.Management.Automation" Version="7.0.0" />
<PackageReference Include="System.Management.Automation" Version="7.2.1" />
</ItemGroup>

</Project>
35 changes: 19 additions & 16 deletions Cnct/Cnct.Core/Configuration/LinkTaskSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ public IDictionary<string, IEnumerable<string>> GetLinkConfigurations(string con
break;

case SymlinkSpecification spec:
string[] destinationPaths = null;
switch (Platform.CurrentPlatform)
string[] platformLinkPaths = Platform.CurrentPlatform switch
{
case PlatformType.Windows:
destinationPaths = GetPlatformLinkPaths(target, spec.Windows);
break;
case PlatformType.Linux:
destinationPaths = GetPlatformLinkPaths(target, spec.Linux);
break;
case PlatformType.OSX:
destinationPaths = GetPlatformLinkPaths(target, spec.Osx);
break;
PlatformType.Windows => spec.Windows,
PlatformType.Linux => spec.Linux,
PlatformType.OSX => spec.Osx,
_ => throw new NotImplementedException(),
};

string[] destinationPaths;
if (TryGetPlatformLinkPaths(target, platformLinkPaths, out destinationPaths))
{
linkConfigs.Add(target, destinationPaths);
}

if (destinationPaths != null)
if (Platform.CurrentPlatformIsUnix && TryGetPlatformLinkPaths(target, spec.Unix, out destinationPaths))
{
linkConfigs.Add(target, destinationPaths);
}
Expand All @@ -73,19 +73,22 @@ public async Task ExecuteAsync(ILogger logger, string confgiDirectoryRoot)
await linkTask.ExecuteAsync();
}

private static string[] GetPlatformLinkPaths(string target, string[] platformLinkPaths)
private static bool TryGetPlatformLinkPaths(string target, string[] platformLinkPaths, out string[] destinationLinks)
{
if (platformLinkPaths == null)
{
return null;
destinationLinks = null;
return false;
}
else if (platformLinkPaths.Length == 0)
{
return new[] { GetDotFileLinkPath(target) };
destinationLinks = new[] { GetDotFileLinkPath(target) };
return true;
}
else
{
return platformLinkPaths.Select(p => p.NormalizePath()).ToArray();
destinationLinks = platformLinkPaths.Select(p => p.NormalizePath()).ToArray();
return true;
}
}

Expand Down
14 changes: 14 additions & 0 deletions Cnct/Cnct.Core/Configuration/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public static string Home

public static PlatformType CurrentPlatform => CurrentPlatformLazy.Value;

public static bool CurrentPlatformIsUnix { get; } = IsUnix();

private static bool IsUnix()
{
switch (CurrentPlatform)
{
case PlatformType.Linux:
case PlatformType.OSX:
return true;
default:
return false;
}
}

private static PlatformType GetCurrentPlatformType()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down
3 changes: 3 additions & 0 deletions Cnct/Cnct.Core/Configuration/SymlinkSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public class SymlinkSpecification

[JsonConverter(typeof(LinkCollectionConverter))]
public string[] Linux { get; set; }

[JsonConverter(typeof(LinkCollectionConverter))]
public string[] Unix { get; set; }
}
}
6 changes: 4 additions & 2 deletions Cnct/Cnct.Core/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.InteropServices;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Cnct.Core.Tasks;

namespace Cnct.Core
Expand All @@ -11,7 +13,7 @@ public static extern bool CreateSymbolicLink(
string targetFileName,
LinkType flags);

[DllImport("libc.so.6", EntryPoint = "symlink", CharSet = CharSet.Unicode)]
[DllImport("libc", EntryPoint = "symlink", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern int CreateLinuxSymlink(string targetPath, string linkPath);
}
}
3 changes: 2 additions & 1 deletion Cnct/Cnct.Core/Tasks/LinkTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ private void CreateLink(string linkPath, string targetPath, LinkType linkType)
case PlatformType.Linux:
if (NativeMethods.CreateLinuxSymlink(targetPath, linkPath) != 0)
{
this.Logger.LogError($"Failed to create link.");
var errno = Marshal.GetLastWin32Error();
this.Logger.LogError($"Failed to create link (errno: {errno}).");
}

break;
Expand Down
6 changes: 3 additions & 3 deletions Cnct/Cnct.NetCore/Cnct.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<PackAsTool>true</PackAsTool>
<ToolCommandName>cnct</ToolCommandName>
Expand All @@ -20,15 +20,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Install your dotfiles, no matter what platform you're on.

## Build Status

| Branch | |
|:-------------|:----------------------------|
| **develop** | ![Build status][ci-develop] |
| **main** | ![Build status][ci-main] |
| Branch | |
|:-------------|:--------------------------------------------------|
| **develop** | [![Build status][ci-develop]][ci-develop-history] |
| **main** | [![Build status][ci-main]][ci-main-history] |

## Overview

Expand All @@ -26,7 +26,7 @@ $c = Get-Credential -UserName "<your GitHub username>"
dotnet nuget add source --name github-bgold09 "https://nuget.pkg.github.com/bgold09/index.json" `
--username $c.UserName --password $c.GetNetworkCredential().Password
dotnet tool install --global cnct --source github-bgold09
dotnet tool install --global cnct
```

## Configuration file
Expand Down Expand Up @@ -59,3 +59,5 @@ cnct -c ~/.dotfiles/cnct.json
[create-pat]: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
[ci-develop]: https://github.com/bgold09/cnct-net/actions/workflows/actions-main.yml/badge.svg?branch=develop
[ci-main]: https://github.com/bgold09/cnct-net/actions/workflows/actions-main.yml/badge.svg?branch=main
[ci-develop-history]: https://github.com/bgold09/cnct-net/actions?query=event%3Apush+branch%3Adevelop
[ci-main-history]: https://github.com/bgold09/cnct-net/actions?query=event%3Apush+branch%3Amain
36 changes: 0 additions & 36 deletions azure-pipelines.yml

This file was deleted.

Loading

0 comments on commit 354fa9e

Please sign in to comment.