From 47076ae1e3ff7166c6c96914e3844dd51b66b234 Mon Sep 17 00:00:00 2001 From: Takym <15681312+Takym@users.noreply.github.com> Date: Tue, 29 Jun 2021 15:30:11 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=82=BB=E3=83=B3?= =?UTF-8?q?=E3=82=B9=E9=80=9A=E7=9F=A5=E3=81=AE=E6=9C=89=E7=84=A1=E3=82=92?= =?UTF-8?q?=E6=A4=9C=E8=A8=BC=E3=81=99=E3=82=8B=E3=83=84=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tools/LicenseHeader/Validate.csx | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Tools/LicenseHeader/Validate.csx diff --git a/Tools/LicenseHeader/Validate.csx b/Tools/LicenseHeader/Validate.csx new file mode 100644 index 000000000..6296ebec1 --- /dev/null +++ b/Tools/LicenseHeader/Validate.csx @@ -0,0 +1,99 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#nullable enable + +if (Args.Count != 1) { + WriteLine("対象のディレクトリを指定してください。"); + return; +} + +string dir = Path.GetFullPath(Args[0]); +string[] files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories); +var invalid_files = new List(); + +WriteLine("対象のディレクトリ:" + dir); +WriteLine(); + +for (int i = 0; i < files.Length; ++i) { + string file = files[i]; + string ext = Path.GetExtension(file); + WriteLine("\"{0}\" を確認しています . . .", file); + if (!Ignore(file) && ext is ".cs" or ".csx" or ".strings" or ".xml" or ".xaml" or ".resx" + or ".axml" or ".storyboard" or ".bat" or ".cmd" or ".sh" or ".tf" or ".yml" or ".feature") { + using (var fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) + using (var sr = new StreamReader(fs, true)) { + if (Validate(sr.ReadToEnd())) { + WriteLine("ライセンス通知は記述されています。"); + } else { + invalid_files.Add(file); + ForegroundColor = ConsoleColor.Red; + WriteLine("ライセンス通知は記述されていません。"); + ResetColor(); + } + } + } else { + WriteLine("このファイルは無視されます。"); + } + WriteLine(); +} + +WriteLine(); + +int count = invalid_files.Count; +if (count == 0) { + WriteLine("ライセンス通知は全てのファイルに記述されています。"); +} else { + var sb = new StringBuilder(); + sb.AppendLine("ライセンス通知が記述されていないファイルがあります。"); + for (int i = 0; i < count; ++i) { + sb.Append(" - ").AppendLine(invalid_files[i]); + } + throw new LicenseHeaderException(sb.ToString()); +} + + +/*================================================================================================*/ + +const string HEADER_LINE_1 = "This Source Code Form is subject to the terms of the Mozilla Public"; +const string HEADER_LINE_2 = "License, v. 2.0. If a copy of the MPL was not distributed with this"; +const string HEADER_LINE_3 = "file, You can obtain one at http://mozilla.org/MPL/2.0/."; +const string HEADER_LINE_3s = "file, You can obtain one at https://mozilla.org/MPL/2.0/."; + +/// 検証 +public static bool Validate(string data) +{ + if ((data.Contains(HEADER_LINE_1) && data.Contains(HEADER_LINE_2)) && + (data.Contains(HEADER_LINE_3) || data.Contains(HEADER_LINE_3s))) { + return true; + } else { + return false; + } +} + +/// 除外設定 +public static bool Ignore(string file) +{ + return file.EndsWith(".designer.cs") + || file.EndsWith(".Designer.cs") + || file.EndsWith(".feature.cs") + || file.Contains("Xamarin.ExposureNotification") + || file.Contains("bin") + || file.Contains("Bin") + || file.Contains("obj") + || file.Contains("Obj") + || file.Contains(".github"); +} + +[Serializable()] +public sealed class LicenseHeaderException : System.Exception +{ + public LicenseHeaderException(string message) + : base(message) { } + + protected LicenseHeaderException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base(info, context) { } +} From 50602f4148f13a1cf7cb6d1e5cbb54165813409d Mon Sep 17 00:00:00 2001 From: Takym <15681312+Takym@users.noreply.github.com> Date: Tue, 29 Jun 2021 15:38:46 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=82=BB=E3=83=B3?= =?UTF-8?q?=E3=82=B9=E9=80=9A=E7=9F=A5=E3=81=AE=E6=9C=89=E7=84=A1=E3=82=92?= =?UTF-8?q?=E6=A4=9C=E8=A8=BC=E3=81=99=E3=82=8BCI=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/CheckLicense.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/CheckLicense.yml diff --git a/.github/workflows/CheckLicense.yml b/.github/workflows/CheckLicense.yml new file mode 100644 index 000000000..e921367ee --- /dev/null +++ b/.github/workflows/CheckLicense.yml @@ -0,0 +1,21 @@ +name: "Validate License Header" + +on: + push: + branches: [ main, develop, 'feature/**' ] + pull_request: + branches: [ main, develop, 'feature/**' ] + +jobs: + test: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + - name: Setup .NET + uses: actions/setup-dotnet@v1.8.1 + with: + dotnet-version: 5.0.301 + - name: Setup C# Interactive + run: dotnet tool install dotnet-script + - name: Validate + run: dotnet script Tools/LicenseHeader/Validate.csx . From d580fad8683ca1bd0f561b0d3b04104c6e33b5a9 Mon Sep 17 00:00:00 2001 From: Takym <15681312+Takym@users.noreply.github.com> Date: Tue, 29 Jun 2021 15:45:55 +0900 Subject: [PATCH 3/5] =?UTF-8?q?CI=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/CheckLicense.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CheckLicense.yml b/.github/workflows/CheckLicense.yml index e921367ee..4a06708c0 100644 --- a/.github/workflows/CheckLicense.yml +++ b/.github/workflows/CheckLicense.yml @@ -16,6 +16,6 @@ jobs: with: dotnet-version: 5.0.301 - name: Setup C# Interactive - run: dotnet tool install dotnet-script + run: dotnet tool install -g dotnet-script - name: Validate run: dotnet script Tools/LicenseHeader/Validate.csx . From 57a380c1401fab519fd84da9a9ad27f4877c6fb8 Mon Sep 17 00:00:00 2001 From: Takym <15681312+Takym@users.noreply.github.com> Date: Tue, 29 Jun 2021 15:51:30 +0900 Subject: [PATCH 4/5] =?UTF-8?q?`Tools/LicenseHeader/Validate.csx`=20?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=82=BB=E3=83=B3=E3=82=B9=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E3=81=AE=E7=84=A1=E3=81=84=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=82=92=E6=A8=99=E6=BA=96=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tools/LicenseHeader/Validate.csx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tools/LicenseHeader/Validate.csx b/Tools/LicenseHeader/Validate.csx index 6296ebec1..529529218 100644 --- a/Tools/LicenseHeader/Validate.csx +++ b/Tools/LicenseHeader/Validate.csx @@ -50,6 +50,8 @@ if (count == 0) { for (int i = 0; i < count; ++i) { sb.Append(" - ").AppendLine(invalid_files[i]); } + ForegroundColor = ConsoleColor.Red; + WriteLine(sb.ToString()); throw new LicenseHeaderException(sb.ToString()); } From 2901380eedc930201d9c978ebdb7246ef6a95a19 Mon Sep 17 00:00:00 2001 From: Takym <15681312+Takym@users.noreply.github.com> Date: Tue, 29 Jun 2021 15:56:25 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BE=8B=E5=A4=96=E3=82=92=E5=BB=83?= =?UTF-8?q?=E6=AD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tools/LicenseHeader/Validate.csx | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/Tools/LicenseHeader/Validate.csx b/Tools/LicenseHeader/Validate.csx index 529529218..81b152402 100644 --- a/Tools/LicenseHeader/Validate.csx +++ b/Tools/LicenseHeader/Validate.csx @@ -52,7 +52,7 @@ if (count == 0) { } ForegroundColor = ConsoleColor.Red; WriteLine(sb.ToString()); - throw new LicenseHeaderException(sb.ToString()); + Environment.Exit(-1); } @@ -87,15 +87,3 @@ public static bool Ignore(string file) || file.Contains("Obj") || file.Contains(".github"); } - -[Serializable()] -public sealed class LicenseHeaderException : System.Exception -{ - public LicenseHeaderException(string message) - : base(message) { } - - protected LicenseHeaderException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) - : base(info, context) { } -}