diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..19a3a33 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: csharp +mono: none +dotnet: 5.0.302 +solution: Castoreum.sln +before_install: + - dotnet --info +install: + - dotnet restore + - dotnet tool install dotnet-format +script: + - dotnet test + - dotnet dotnet-format --check + - dotnet publish \ No newline at end of file diff --git a/Castoreum.Builder/Castoreum.Builder.csproj b/Castoreum.Builder/Castoreum.Builder.csproj deleted file mode 100644 index 2f002aa..0000000 --- a/Castoreum.Builder/Castoreum.Builder.csproj +++ /dev/null @@ -1,7 +0,0 @@ - - - - net5.0 - - - diff --git a/Castoreum.Compression/Castoreum.Compression.csproj b/Castoreum.Compression/Castoreum.Compression.csproj index f208d30..cf1d5b8 100644 --- a/Castoreum.Compression/Castoreum.Compression.csproj +++ b/Castoreum.Compression/Castoreum.Compression.csproj @@ -1,7 +1,11 @@ - + net5.0 + + + + diff --git a/Castoreum.Compression/CompressionManager.cs b/Castoreum.Compression/CompressionManager.cs new file mode 100644 index 0000000..313574a --- /dev/null +++ b/Castoreum.Compression/CompressionManager.cs @@ -0,0 +1,49 @@ +using Castoreum.Interface.Service.Compression; +using Castoreum.Interface.Service.Config; +using System.IO; +using System.IO.Compression; + +namespace Castoreum.Compression +{ + public class CompressionManager : ICompressionManager + { + public void BuildMod(ZipArchive archive, IConfig config, DirectoryInfo directoryInfo) + { + string[] directoryPath = Directory.GetCurrentDirectory().Split('\\'); + string rootDirectory = directoryPath[^1]; + string formattedPath = directoryInfo.FullName.Split(rootDirectory)[1].Remove(0, 1); + + var files = directoryInfo.GetFiles(); + foreach (var file in files) + { + if (config.Type == "module" || config.Type == "package") + { + archive.CreateEntryFromFile(file.FullName, $"modules\\{config.RepoName}\\{formattedPath}\\{file.Name}"); + } + archive.CreateEntryFromFile(file.FullName, $"{formattedPath}\\{file.Name}"); + } + var directories = directoryInfo.GetDirectories(); + bool exclude; + string subDirectoryPath; + foreach (var directory in directories) + { + exclude = false; + subDirectoryPath = directory.FullName.Split(rootDirectory)[1].Remove(0, 1); + foreach (var excludedPath in config.ExcludeFolders) + { + if (subDirectoryPath == excludedPath || subDirectoryPath == excludedPath.Replace('/', '\\')) + exclude = true; + } + + foreach (var dependencyPath in config.Dependencies) + { + if (subDirectoryPath == dependencyPath || subDirectoryPath == dependencyPath.Replace('/', '\\')) + exclude = true; + } + + if (!exclude) + BuildMod(archive, config, directory); + } + } + } +} diff --git a/Castoreum.Config/Castoreum.Config.csproj b/Castoreum.Config/Castoreum.Config.csproj index cf1d5b8..438f47d 100644 --- a/Castoreum.Config/Castoreum.Config.csproj +++ b/Castoreum.Config/Castoreum.Config.csproj @@ -4,6 +4,10 @@ net5.0 + + + + diff --git a/Castoreum.Config/Service/ConfigManager.cs b/Castoreum.Config/Service/ConfigManager.cs index dd3370b..25c1431 100644 --- a/Castoreum.Config/Service/ConfigManager.cs +++ b/Castoreum.Config/Service/ConfigManager.cs @@ -1,7 +1,8 @@ using Castoreum.Config.Models; using Castoreum.Interface.Service.Config; -using System; +using Newtonsoft.Json.Linq; using System.IO; +using System.Text.Json; namespace Castoreum.Config.Service { @@ -17,12 +18,14 @@ public IConfig CreateConfigFile(string archiveName) public IConfig GetConfig(string file) { - throw new NotImplementedException(); + string castorConfigText = File.ReadAllText(file); + return JsonSerializer.Deserialize(castorConfigText); } - public void PlaceConfigFile(IConfig config) + public void PlaceConfigFile(IConfig config, string fileName) { - throw new NotImplementedException(); + string newJson = JToken.Parse(JsonSerializer.Serialize(config)).ToString(); + File.WriteAllText(fileName, newJson); } public void PlaceGitIgnore(string path) diff --git a/Castoreum.Installation/Castoreum.Installation.csproj b/Castoreum.Installation/Castoreum.Installation.csproj index f208d30..73e4800 100644 --- a/Castoreum.Installation/Castoreum.Installation.csproj +++ b/Castoreum.Installation/Castoreum.Installation.csproj @@ -4,4 +4,14 @@ net5.0 + + + + + + + + + + diff --git a/Castoreum.Installation/InstallationManager.cs b/Castoreum.Installation/InstallationManager.cs new file mode 100644 index 0000000..ffb0f73 --- /dev/null +++ b/Castoreum.Installation/InstallationManager.cs @@ -0,0 +1,108 @@ +using Castoreum.Config.Models; +using Castoreum.Interface.Service.Config; +using Castoreum.Interface.Service.Installation; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Net; + +namespace Castoreum.Installation +{ + public class InstallationManager : IInstallationManager + { + public IConfig InstallDependency(IConfig config, string packageName) + { + InstallPackage(packageName); + List dependencies = (List)config.Dependencies; + if (!dependencies.Contains(packageName)) + { + dependencies.Add(packageName); + } + return config; + } + + public IConfig InstallDevDependency(IConfig config, string packageName) + { + InstallPackage(packageName); + List dependencies = (List)config.DevDependencies; + if (!dependencies.Contains(packageName)) + { + dependencies.Add(packageName); + } + return config; + } + + public IConfig RemoveDependency(IConfig config, string packageName) + { + UninstallPackage(packageName); + List dependencies = (List)config.Dependencies; + if (!dependencies.Contains(packageName)) + { + dependencies.RemoveAll(p => p == packageName); + } + return config; + } + + public IConfig RemoveDevDependency(IConfig config, string packageName) + { + UninstallPackage(packageName); + List dependencies = (List)config.DevDependencies; + if (!dependencies.Contains(packageName)) + { + dependencies.RemoveAll(p => p == packageName); + } + return config; + } + + public void InstallPackage(string packageName) + { + using WebClient client = new(); + Guid guid = Guid.NewGuid(); + string[] packageArgs = packageName.Split('/'); + client.DownloadFile($"https://github.com/{packageArgs[0]}/{packageArgs[1]}/archive/refs/tags/{packageArgs[2]}.zip", $"{guid}.zip"); + ZipFile.ExtractToDirectory($"{guid}.zip", $"{guid}"); + + // remove the package's modules folder if the dev didn't exclude it + if (Directory.Exists($"{guid}/modules")) + Directory.Delete($"{guid}/modules"); + + string[] subDirectories = Directory.GetDirectories($"{guid}"); + string firstSubDir = ""; + if (subDirectories.Length > 0) + { + firstSubDir = subDirectories[0]; + } + + CopyFilesRecursively($"{guid}/{firstSubDir.Split('\\')[1]}", $"modules/{packageArgs[0]}/{packageArgs[1]}"); + + var directory = new DirectoryInfo($"{guid}") { Attributes = FileAttributes.Normal }; + foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories)) + { + info.Attributes = FileAttributes.Normal; + } + + Directory.Delete($"{guid}", true); + File.Delete($"{guid}.zip"); + } + + private void UninstallPackage(string packageName) + { + if (Directory.Exists($"modules/{packageName}")) + Directory.Delete($"modules/{packageName}"); + } + + private static void CopyFilesRecursively(string sourcePath, string targetPath) + { + foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) + { + Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); + } + + foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) + { + File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true); + } + } + } +} diff --git a/Castoreum.Interface/Service/Builder/IBuildManager.cs b/Castoreum.Interface/Service/Builder/IBuildManager.cs deleted file mode 100644 index 0510921..0000000 --- a/Castoreum.Interface/Service/Builder/IBuildManager.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Castoreum.Interface.Service.Builder -{ - public interface IBuildManager - { - void BuildMod(string path, bool isZ2f); - void BuildPackage(string path, bool isZ2f); - } -} diff --git a/Castoreum.Interface/Service/Compression/ICompressionManager.cs b/Castoreum.Interface/Service/Compression/ICompressionManager.cs index 2ae27f1..30b1f8e 100644 --- a/Castoreum.Interface/Service/Compression/ICompressionManager.cs +++ b/Castoreum.Interface/Service/Compression/ICompressionManager.cs @@ -6,7 +6,6 @@ namespace Castoreum.Interface.Service.Compression { public interface ICompressionManager { - void CreateArchive(ZipArchive archive, IConfig config, DirectoryInfo directoryInfo); - void AddToArchive(ZipArchive archive, IConfig config, DirectoryInfo directoryInfo); + void BuildMod(ZipArchive archive, IConfig config, DirectoryInfo directoryInfo); } } diff --git a/Castoreum.Interface/Service/Config/IConfigManager.cs b/Castoreum.Interface/Service/Config/IConfigManager.cs index cdf0310..ab22e9b 100644 --- a/Castoreum.Interface/Service/Config/IConfigManager.cs +++ b/Castoreum.Interface/Service/Config/IConfigManager.cs @@ -3,7 +3,7 @@ public interface IConfigManager { IConfig CreateConfigFile(string archiveName); - void PlaceConfigFile(IConfig config); + void PlaceConfigFile(IConfig config, string fileName); void PlaceGitIgnore(string path); IConfig GetConfig(string file); } diff --git a/Castoreum.Interface/Service/Installation/IInstallationManager.cs b/Castoreum.Interface/Service/Installation/IInstallationManager.cs index 382236a..319f7c3 100644 --- a/Castoreum.Interface/Service/Installation/IInstallationManager.cs +++ b/Castoreum.Interface/Service/Installation/IInstallationManager.cs @@ -4,8 +4,10 @@ namespace Castoreum.Interface.Service.Installation { public interface IInstallationManager { - void InstallDependency(string packageName, IConfig config); - void InstallDevDependency(string packageName, IConfig config); - void RemoveDependency(string packageName, IConfig config); + IConfig InstallDependency(IConfig config, string packageName); + IConfig InstallDevDependency(IConfig config, string packageName); + void InstallPackage(string packageName); + IConfig RemoveDependency(IConfig config, string packageName); + IConfig RemoveDevDependency(IConfig config, string packageName); } } diff --git a/Castoreum.Interface/Service/Watch/IProcessWatcher.cs b/Castoreum.Interface/Service/Watch/IProcessWatcher.cs index 39a37be..838ffd3 100644 --- a/Castoreum.Interface/Service/Watch/IProcessWatcher.cs +++ b/Castoreum.Interface/Service/Watch/IProcessWatcher.cs @@ -8,8 +8,6 @@ namespace Castoreum.Interface.Service.Watch { public interface IProcessWatcher { - void LaunchProcess(); - void EndProcess(); - void Watch(); + void Watch(string program, string arg); } } diff --git a/Castoreum.Tests/Castoreum.Tests.csproj b/Castoreum.Tests/Castoreum.Tests.csproj deleted file mode 100644 index cc7744a..0000000 --- a/Castoreum.Tests/Castoreum.Tests.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - net5.0 - - false - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - diff --git a/Castoreum.Watch/Castoreum.Watch.csproj b/Castoreum.Watch/Castoreum.Watch.csproj index 2f002aa..4cb9554 100644 --- a/Castoreum.Watch/Castoreum.Watch.csproj +++ b/Castoreum.Watch/Castoreum.Watch.csproj @@ -1,7 +1,11 @@ - + net5.0 + + + + diff --git a/Castoreum.Watch/ProcessWatcher.cs b/Castoreum.Watch/ProcessWatcher.cs new file mode 100644 index 0000000..baac0a7 --- /dev/null +++ b/Castoreum.Watch/ProcessWatcher.cs @@ -0,0 +1,32 @@ +using Castoreum.Interface.Service.Watch; +using System; +using System.Diagnostics; +using System.IO; + +namespace Castoreum.Watch +{ + public class ProcessWatcher : IProcessWatcher + { + public void Watch(string program, string arg) + { + using var process = new Process(); + process.StartInfo.FileName = program; + process.StartInfo.Arguments = arg; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.UseShellExecute = false; + process.Start(); + + using (StreamWriter writer = new("castorlog.txt")) + { + while (!process.StandardOutput.EndOfStream) + { + string line = process.StandardOutput.ReadLine(); + Console.WriteLine($"{line}"); + writer.WriteLine($"{line}"); + } + } + + process.WaitForExit(); + } + } +} diff --git a/Castoreum.sln b/Castoreum.sln index c21341c..266726b 100644 --- a/Castoreum.sln +++ b/Castoreum.sln @@ -9,14 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castoreum.Installation", "C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castoreum.Watch", "Castoreum.Watch\Castoreum.Watch.csproj", "{6ABA49E7-B5D2-4D46-BD97-5C93875C5E2A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castoreum.Builder", "Castoreum.Builder\Castoreum.Builder.csproj", "{D0494078-94AE-480B-8E7F-A0DB5AD17C8C}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castoreum.Config", "Castoreum.Config\Castoreum.Config.csproj", "{B2145345-8F51-4468-9E92-E82811CC76BA}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castoreum.Interface", "Castoreum.Interface\Castoreum.Interface.csproj", "{7C4CADB7-F660-423B-82D1-F4F95071A8C3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Castoreum.Tests", "Castoreum.Tests\Castoreum.Tests.csproj", "{82F0844E-E942-4292-AF4F-87C6E4C173D7}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,10 +31,6 @@ Global {6ABA49E7-B5D2-4D46-BD97-5C93875C5E2A}.Debug|Any CPU.Build.0 = Debug|Any CPU {6ABA49E7-B5D2-4D46-BD97-5C93875C5E2A}.Release|Any CPU.ActiveCfg = Release|Any CPU {6ABA49E7-B5D2-4D46-BD97-5C93875C5E2A}.Release|Any CPU.Build.0 = Release|Any CPU - {D0494078-94AE-480B-8E7F-A0DB5AD17C8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D0494078-94AE-480B-8E7F-A0DB5AD17C8C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D0494078-94AE-480B-8E7F-A0DB5AD17C8C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D0494078-94AE-480B-8E7F-A0DB5AD17C8C}.Release|Any CPU.Build.0 = Release|Any CPU {B2145345-8F51-4468-9E92-E82811CC76BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B2145345-8F51-4468-9E92-E82811CC76BA}.Debug|Any CPU.Build.0 = Debug|Any CPU {B2145345-8F51-4468-9E92-E82811CC76BA}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -47,10 +39,6 @@ Global {7C4CADB7-F660-423B-82D1-F4F95071A8C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {7C4CADB7-F660-423B-82D1-F4F95071A8C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {7C4CADB7-F660-423B-82D1-F4F95071A8C3}.Release|Any CPU.Build.0 = Release|Any CPU - {82F0844E-E942-4292-AF4F-87C6E4C173D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {82F0844E-E942-4292-AF4F-87C6E4C173D7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {82F0844E-E942-4292-AF4F-87C6E4C173D7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {82F0844E-E942-4292-AF4F-87C6E4C173D7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/dotnet-tools.json b/dotnet-tools.json new file mode 100644 index 0000000..d8d5a3b --- /dev/null +++ b/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-format": { + "version": "5.1.250801", + "commands": [ + "dotnet-format" + ] + } + } +} \ No newline at end of file