ShareX/ShareX.Setup/Program.cs

406 lines
15 KiB
C#
Raw Normal View History

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2022-01-12 05:32:17 +13:00
Copyright (c) 2007-2022 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2018-03-03 10:06:40 +13:00
using ShareX.HelpersLib;
using System;
using System.Diagnostics;
using System.IO;
2014-12-11 09:25:20 +13:00
namespace ShareX.Setup
{
internal class Program
{
2016-08-27 12:28:05 +12:00
[Flags]
private enum SetupJobs
{
2016-08-27 12:28:05 +12:00
None = 0,
CreateSetup = 1,
CreatePortable = 1 << 1,
2022-10-23 18:46:34 +13:00
CreateDebug = 1 << 2,
CreateSteamFolder = 1 << 3,
CreateMicrosoftStoreFolder = 1 << 4,
CreateMicrosoftStoreDebugFolder = 1 << 5,
CompileAppx = 1 << 6,
DownloadFFmpeg = 1 << 7,
CreateChecksumFile = 1 << 8,
2022-10-23 18:46:34 +13:00
OpenOutputDirectory = 1 << 9,
2016-08-27 12:28:05 +12:00
2022-10-24 16:56:59 +13:00
Release = CreateSetup | CreatePortable | DownloadFFmpeg | CreateChecksumFile | OpenOutputDirectory,
Debug = CreateDebug | DownloadFFmpeg | CreateChecksumFile | OpenOutputDirectory,
Steam = CreateSteamFolder | DownloadFFmpeg | CreateChecksumFile | OpenOutputDirectory,
MicrosoftStore = CreateMicrosoftStoreFolder | CompileAppx | DownloadFFmpeg | CreateChecksumFile | OpenOutputDirectory,
MicrosoftStoreDebug = CreateMicrosoftStoreDebugFolder | CompileAppx | DownloadFFmpeg | CreateChecksumFile | OpenOutputDirectory
}
2022-10-24 16:56:59 +13:00
private static SetupJobs Job = SetupJobs.Release;
2016-08-27 12:28:05 +12:00
private static bool AppVeyor = false;
2015-09-28 01:46:29 +13:00
2022-10-23 19:31:26 +13:00
private static string Configuration
{
get
{
2022-10-23 19:31:26 +13:00
if (Job.HasFlag(SetupJobs.CreateDebug))
{
2022-10-23 19:31:26 +13:00
return "Debug";
2022-10-14 13:05:45 +13:00
}
2022-10-23 19:31:26 +13:00
else if (Job.HasFlag(SetupJobs.CreateSteamFolder))
2022-10-23 18:46:34 +13:00
{
2022-10-23 19:31:26 +13:00
return "Steam";
2022-10-23 18:46:34 +13:00
}
2022-10-14 13:05:45 +13:00
else if (Job.HasFlag(SetupJobs.CreateMicrosoftStoreFolder))
{
2022-10-23 19:31:26 +13:00
return "MicrosoftStore";
2022-10-14 13:05:45 +13:00
}
else if (Job.HasFlag(SetupJobs.CreateMicrosoftStoreDebugFolder))
{
2022-10-23 19:31:26 +13:00
return "MicrosoftStoreDebug";
}
2022-10-23 19:31:26 +13:00
return "Release";
}
}
2022-10-23 19:31:26 +13:00
private static string ParentDir => AppVeyor ? "." : @"..\..\..\";
private static string BinDir => Path.Combine(ParentDir, "ShareX", "bin", Configuration);
private static string NativeMessagingHostDir => Path.Combine(ParentDir, "ShareX.NativeMessagingHost", "bin", Configuration);
private static string SteamLauncherDir => Path.Combine(ParentDir, "ShareX.Steam", "bin", Configuration);
private static string ExecutablePath => Path.Combine(BinDir, "ShareX.exe");
private static string OutputDir => Path.Combine(ParentDir, "Output");
2016-08-27 12:02:43 +12:00
private static string PortableOutputDir => Path.Combine(OutputDir, "ShareX-portable");
2022-10-23 18:46:34 +13:00
private static string DebugOutputDir => Path.Combine(OutputDir, "ShareX-debug");
private static string SteamOutputDir => Path.Combine(OutputDir, "ShareX-Steam");
private static string MicrosoftStoreOutputDir => Path.Combine(OutputDir, "ShareX-MicrosoftStore");
2017-04-23 05:38:19 +12:00
private static string SetupDir => Path.Combine(ParentDir, "ShareX.Setup");
private static string InnoSetupDir => Path.Combine(SetupDir, "InnoSetup");
private static string MicrosoftStorePackageFilesDir => Path.Combine(SetupDir, "MicrosoftStore");
2016-08-27 12:02:43 +12:00
2022-11-02 01:36:31 +13:00
private static string SetupPath => Path.Combine(OutputDir, $"ShareX-{AppVersion}-setup.exe");
private static string RecorderDevicesSetupPath => Path.Combine(OutputDir, "Recorder-devices-setup.exe");
2022-10-14 11:44:23 +13:00
private static string PortableZipPath => Path.Combine(OutputDir, $"ShareX-{AppVersion}-portable.zip");
2022-10-23 18:46:34 +13:00
private static string DebugZipPath => Path.Combine(OutputDir, $"ShareX-{AppVersion}-debug.zip");
2016-08-26 13:51:37 +12:00
private static string SteamUpdatesDir => Path.Combine(SteamOutputDir, "Updates");
2022-10-14 11:44:23 +13:00
private static string SteamZipPath => Path.Combine(OutputDir, $"ShareX-{AppVersion}-Steam.zip");
private static string MicrosoftStoreAppxPath => Path.Combine(OutputDir, $"ShareX-{AppVersion}.appx");
private static string FFmpegPath => Path.Combine(OutputDir, "ffmpeg.exe");
2016-08-27 12:02:43 +12:00
private static string InnoSetupCompilerPath = @"C:\Program Files (x86)\Inno Setup 6\ISCC.exe";
private static string MakeAppxPath = @"C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\makeappx.exe";
2022-10-14 11:44:23 +13:00
private static string AppVersion;
private static void Main(string[] args)
{
2016-08-27 12:28:05 +12:00
Console.WriteLine("ShareX setup started.");
2016-08-26 12:01:05 +12:00
2022-10-15 11:28:50 +13:00
CheckArgs(args);
2016-08-27 12:28:05 +12:00
Console.WriteLine("Setup job: " + Job);
2022-10-14 12:35:03 +13:00
AppVersion = GetAppVersion();
2016-08-27 12:32:02 +12:00
if (Directory.Exists(OutputDir))
{
Console.WriteLine("Cleaning output directory: " + OutputDir);
Directory.Delete(OutputDir, true);
}
if (Job.HasFlag(SetupJobs.DownloadFFmpeg))
{
DownloadFFmpeg();
}
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.CreateSetup))
{
CompileSetup();
}
if (Job.HasFlag(SetupJobs.CreatePortable))
2016-08-26 12:01:05 +12:00
{
2022-10-23 19:31:26 +13:00
CreateFolder(BinDir, PortableOutputDir, SetupJobs.CreatePortable);
2016-08-26 12:01:05 +12:00
}
2022-10-23 18:46:34 +13:00
if (Job.HasFlag(SetupJobs.CreateDebug))
{
2022-10-23 19:31:26 +13:00
CreateFolder(BinDir, DebugOutputDir, SetupJobs.CreateDebug);
2022-10-23 18:46:34 +13:00
}
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.CreateSteamFolder))
{
CreateSteamFolder();
}
if (Job.HasFlag(SetupJobs.CreateMicrosoftStoreFolder))
2017-04-21 11:17:34 +12:00
{
2022-10-23 19:31:26 +13:00
CreateFolder(BinDir, MicrosoftStoreOutputDir, SetupJobs.CreateMicrosoftStoreFolder);
2017-04-22 05:08:29 +12:00
}
if (Job.HasFlag(SetupJobs.CreateMicrosoftStoreDebugFolder))
2017-04-22 05:08:29 +12:00
{
2022-10-23 19:31:26 +13:00
CreateFolder(BinDir, MicrosoftStoreOutputDir, SetupJobs.CreateMicrosoftStoreDebugFolder);
2017-04-21 11:17:34 +12:00
}
if (Job.HasFlag(SetupJobs.CompileAppx))
{
2022-11-02 01:36:31 +13:00
CompileAppx();
2021-11-10 21:57:10 +13:00
}
if (AppVeyor)
{
FileHelpers.CopyAll(OutputDir, ParentDir);
}
2022-10-24 16:56:59 +13:00
if (Job.HasFlag(SetupJobs.OpenOutputDirectory) && !AppVeyor)
2016-08-27 12:28:05 +12:00
{
2022-10-15 11:46:28 +13:00
FileHelpers.OpenFolder(OutputDir, false);
2016-08-27 12:28:05 +12:00
}
2015-09-06 21:32:34 +12:00
2016-08-27 12:28:05 +12:00
Console.WriteLine("ShareX setup successfully completed.");
2016-08-26 12:01:05 +12:00
}
2022-10-15 11:28:50 +13:00
private static void CheckArgs(string[] args)
{
2022-10-15 11:28:50 +13:00
CLIManager cli = new CLIManager(args);
cli.ParseCommands();
CLICommand command = cli.GetCommand("AppVeyor");
if (command != null)
{
AppVeyor = true;
2022-10-15 11:28:50 +13:00
string configuration = command.Parameter;
Console.WriteLine("AppVeyor: " + configuration);
if (configuration.Equals("Release", StringComparison.OrdinalIgnoreCase))
{
2022-10-24 16:56:59 +13:00
Job = SetupJobs.Release;
2022-10-15 11:28:50 +13:00
}
2022-10-23 18:46:34 +13:00
else if (configuration.Equals("Debug", StringComparison.OrdinalIgnoreCase))
{
2022-10-24 16:56:59 +13:00
Job = SetupJobs.Debug;
2022-10-23 18:46:34 +13:00
}
2022-10-15 11:28:50 +13:00
else if (configuration.Equals("Steam", StringComparison.OrdinalIgnoreCase))
{
2022-10-24 16:56:59 +13:00
Job = SetupJobs.Steam;
2022-10-15 11:28:50 +13:00
}
else if (configuration.Equals("MicrosoftStore", StringComparison.OrdinalIgnoreCase))
{
2022-10-24 16:56:59 +13:00
Job = SetupJobs.MicrosoftStore;
2022-10-15 11:28:50 +13:00
}
else
{
Environment.Exit(0);
}
}
}
2022-10-15 11:46:28 +13:00
private static string GetAppVersion()
{
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(ExecutablePath);
return $"{versionInfo.ProductMajorPart}.{versionInfo.ProductMinorPart}.{versionInfo.ProductBuildPart}";
}
2014-11-22 02:34:50 +13:00
private static void CompileSetup()
{
CompileISSFile("Recorder-devices-setup.iss");
CompileISSFile("ShareX-setup.iss");
2022-11-02 01:36:31 +13:00
CreateChecksumFile(SetupPath);
}
2021-12-12 10:21:19 +13:00
private static void CompileISSFile(string fileName)
2016-08-26 15:41:34 +12:00
{
if (File.Exists(InnoSetupCompilerPath))
{
2021-12-12 10:21:19 +13:00
Console.WriteLine("Compiling setup file: " + fileName);
2016-08-26 15:41:34 +12:00
2018-12-07 07:51:41 +13:00
using (Process process = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = InnoSetupCompilerPath,
WorkingDirectory = Path.GetFullPath(InnoSetupDir),
2021-12-12 10:21:19 +13:00
Arguments = $"\"{fileName}\"",
2018-12-07 07:51:41 +13:00
UseShellExecute = false
};
process.StartInfo = psi;
process.Start();
process.WaitForExit();
}
2016-08-26 15:41:34 +12:00
2022-11-02 01:36:31 +13:00
Console.WriteLine("Setup file compiled.");
}
else
{
Console.WriteLine("InnoSetup compiler is missing: " + InnoSetupCompilerPath);
}
2016-08-26 15:41:34 +12:00
}
2022-11-02 01:36:31 +13:00
private static void CompileAppx()
{
Console.WriteLine("Compiling appx file: " + MicrosoftStoreAppxPath);
using (Process process = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = MakeAppxPath,
Arguments = $"pack /d \"{MicrosoftStoreOutputDir}\" /p \"{MicrosoftStoreAppxPath}\" /l /o",
UseShellExecute = false,
RedirectStandardOutput = true
};
process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
process.StartInfo = psi;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
Console.WriteLine("Appx file compiled.");
2022-11-02 01:51:44 +13:00
CreateChecksumFile(MicrosoftStoreAppxPath);
2022-11-02 01:36:31 +13:00
}
2015-09-06 21:32:34 +12:00
private static void CreateSteamFolder()
{
2022-11-02 01:36:31 +13:00
Console.WriteLine("Creating Steam folder: " + SteamOutputDir);
2016-08-26 13:51:37 +12:00
if (Directory.Exists(SteamOutputDir))
2015-09-06 21:32:34 +12:00
{
2016-08-26 13:51:37 +12:00
Directory.Delete(SteamOutputDir, true);
2015-09-06 21:32:34 +12:00
}
2016-08-26 13:51:37 +12:00
Directory.CreateDirectory(SteamOutputDir);
2015-09-06 21:32:34 +12:00
SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "ShareX_Launcher.exe"), SteamOutputDir);
SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "steam_appid.txt"), SteamOutputDir);
SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "installscript.vdf"), SteamOutputDir);
SetupHelpers.CopyFiles(SteamLauncherDir, "*.dll", SteamOutputDir);
2015-09-06 21:32:34 +12:00
2022-10-23 19:31:26 +13:00
CreateFolder(BinDir, SteamUpdatesDir, SetupJobs.CreateSteamFolder);
2015-09-06 21:32:34 +12:00
}
2017-04-22 05:08:29 +12:00
private static void CreateFolder(string source, string destination, SetupJobs job)
2017-04-21 11:17:34 +12:00
{
2017-04-22 05:08:29 +12:00
Console.WriteLine("Creating folder: " + destination);
2017-04-21 11:17:34 +12:00
2017-04-22 05:08:29 +12:00
if (Directory.Exists(destination))
2017-04-21 11:17:34 +12:00
{
2017-04-22 05:08:29 +12:00
Directory.Delete(destination, true);
2017-04-21 11:17:34 +12:00
}
2017-04-22 05:08:29 +12:00
Directory.CreateDirectory(destination);
2017-04-21 11:17:34 +12:00
SetupHelpers.CopyFile(Path.Combine(source, "ShareX.exe"), destination);
SetupHelpers.CopyFile(Path.Combine(source, "ShareX.exe.config"), destination);
2018-03-03 10:31:06 +13:00
SetupHelpers.CopyFiles(source, "*.dll", destination);
2022-10-23 18:46:34 +13:00
if (job == SetupJobs.CreateDebug || job == SetupJobs.CreateMicrosoftStoreDebugFolder)
2014-11-14 12:28:07 +13:00
{
SetupHelpers.CopyFiles(source, "*.pdb", destination);
2014-11-14 12:28:07 +13:00
}
SetupHelpers.CopyFiles(Path.Combine(ParentDir, "Licenses"), "*.txt", Path.Combine(destination, "Licenses"));
2016-08-28 10:08:18 +12:00
if (job != SetupJobs.CreateMicrosoftStoreFolder && job != SetupJobs.CreateMicrosoftStoreDebugFolder)
{
if (!File.Exists(RecorderDevicesSetupPath))
{
CompileISSFile("Recorder-devices-setup.iss");
}
SetupHelpers.CopyFile(RecorderDevicesSetupPath, destination);
2016-08-28 10:08:18 +12:00
SetupHelpers.CopyFile(Path.Combine(NativeMessagingHostDir, "ShareX_NativeMessagingHost.exe"), destination);
}
2022-07-10 18:54:38 +12:00
string[] languages = new string[] { "de", "es", "es-MX", "fa-IR", "fr", "hu", "id-ID", "it-IT", "ja-JP", "ko-KR", "nl-NL", "pl", "pt-BR", "pt-PT",
2022-06-30 00:01:25 +12:00
"ro", "ru", "tr", "uk", "vi-VN", "zh-CN", "zh-TW" };
2015-01-05 05:53:04 +13:00
foreach (string language in languages)
{
SetupHelpers.CopyFiles(Path.Combine(source, language), "*.resources.dll", Path.Combine(destination, "Languages", language));
2015-01-05 05:53:04 +13:00
}
if (File.Exists(FFmpegPath))
{
SetupHelpers.CopyFile(FFmpegPath, destination);
}
FileHelpers.CopyAll(Path.Combine(ParentDir, @"ShareX.ScreenCaptureLib\Stickers"), Path.Combine(destination, "Stickers"));
2018-03-17 08:58:25 +13:00
2022-10-14 11:44:23 +13:00
if (job == SetupJobs.CreatePortable)
2017-04-21 11:17:34 +12:00
{
2022-10-14 11:44:23 +13:00
FileHelpers.CreateEmptyFile(Path.Combine(destination, "Portable"));
2022-11-02 01:36:31 +13:00
CreateZipFile(destination, PortableZipPath);
}
2022-10-23 18:46:34 +13:00
else if (job == SetupJobs.CreateDebug)
{
2022-11-02 01:36:31 +13:00
CreateZipFile(destination, DebugZipPath);
2022-10-23 18:46:34 +13:00
}
2022-10-14 11:44:23 +13:00
else if (job == SetupJobs.CreateSteamFolder)
2015-09-06 21:32:34 +12:00
{
2022-11-02 01:36:31 +13:00
CreateZipFile(destination, SteamZipPath);
2022-10-14 11:44:23 +13:00
}
else if (job == SetupJobs.CreateMicrosoftStoreFolder || job == SetupJobs.CreateMicrosoftStoreDebugFolder)
{
FileHelpers.CopyAll(MicrosoftStorePackageFilesDir, destination);
2014-01-16 08:12:59 +13:00
}
2017-04-22 05:08:29 +12:00
Console.WriteLine("Folder created.");
}
2014-01-13 05:05:31 +13:00
2022-11-02 01:36:31 +13:00
private static void CreateZipFile(string source, string archivePath)
{
Console.WriteLine("Creating zip file: " + archivePath);
ZipManager.Compress(Path.GetFullPath(source), Path.GetFullPath(archivePath));
CreateChecksumFile(archivePath);
}
private static void DownloadFFmpeg()
2016-08-28 05:57:46 +12:00
{
if (!File.Exists(FFmpegPath))
{
string filePath = SetupHelpers.DownloadFile("https://github.com/ShareX/FFmpeg/releases/download/v5.1/ffmpeg-5.1-win64.zip");
ZipManager.Extract(filePath, OutputDir, false, entry => entry.Name.Equals("ffmpeg.exe", StringComparison.OrdinalIgnoreCase), 200_000_000);
}
2016-08-28 05:57:46 +12:00
}
2022-11-02 01:36:31 +13:00
private static void CreateChecksumFile(string filePath)
{
if (Job.HasFlag(SetupJobs.CreateChecksumFile))
{
Console.WriteLine("Creating checksum file: " + filePath);
Helpers.CreateChecksumFile(filePath);
}
}
}
}