ShareX/ShareX.Setup/Program.cs

319 lines
12 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
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 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)
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
2016-08-26 12:01:05 +12:00
using System.Net;
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,
CreateSteamFolder = 1 << 2,
CreatePortableAppsFolder = 1 << 3,
OpenOutputDirectory = 1 << 4,
UploadOutputFile = 1 << 5,
Stable = CreateSetup | CreatePortable | OpenOutputDirectory,
Setup = CreateSetup | OpenOutputDirectory,
Portable = CreatePortable | OpenOutputDirectory,
Steam = CreateSteamFolder | OpenOutputDirectory,
PortableApps = CreatePortableAppsFolder | OpenOutputDirectory,
Beta = CreateSetup | UploadOutputFile
}
2016-08-27 12:28:05 +12:00
private static SetupJobs Job = SetupJobs.Stable;
private static bool AppVeyor = false;
2015-09-28 01:46:29 +13:00
2016-08-27 12:28:05 +12:00
private static string ParentDir => AppVeyor ? "" : @"..\..\..\";
2016-08-26 13:51:37 +12:00
private static string BinDir => Path.Combine(ParentDir, "ShareX", "bin");
private static string ReleaseDir => Path.Combine(BinDir, "Release");
private static string DebugDir => Path.Combine(BinDir, "Debug");
private static string SteamDir => Path.Combine(BinDir, "Steam");
2016-08-27 12:02:43 +12:00
2016-08-26 13:51:37 +12:00
private static string InnoSetupDir => Path.Combine(ParentDir, @"ShareX.Setup\InnoSetup");
private static string OutputDir => Path.Combine(InnoSetupDir, "Output");
2016-08-27 12:02:43 +12:00
private static string PortableOutputDir => Path.Combine(OutputDir, "ShareX-portable");
private static string SteamOutputDir => Path.Combine(OutputDir, "ShareX-Steam");
2016-08-27 12:02:43 +12:00
private static string PortableAppsOutputDir => Path.Combine(ParentDir, @"..\PortableApps\ShareXPortable\App\ShareX");
2016-08-27 10:55:30 +12:00
private static string SteamLauncherDir => Path.Combine(ParentDir, @"ShareX.Steam\bin\Release");
2016-08-26 13:51:37 +12:00
private static string SteamUpdatesDir => Path.Combine(SteamOutputDir, "Updates");
2016-08-27 12:02:43 +12:00
private static string ChromeDir => Path.Combine(ParentDir, @"ShareX.Chrome\bin\Release");
private static string DebugExecutablePath => Path.Combine(DebugDir, "ShareX.exe");
2016-08-27 06:34:35 +12:00
private static string InnoSetupCompilerPath = @"C:\Program Files (x86)\Inno Setup 5\ISCC.exe";
private static string ZipPath = @"C:\Program Files\7-Zip\7z.exe";
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
2016-08-27 12:28:05 +12:00
AppVeyor = CheckArgs(args, "-appveyor");
if (AppVeyor)
{
Job = SetupJobs.CreateSetup | SetupJobs.CreatePortable;
}
Console.WriteLine("Setup job: " + Job);
if (Job.HasFlag(SetupJobs.CreateSetup))
{
CompileSetup();
}
if (Job.HasFlag(SetupJobs.CreatePortable))
2016-08-26 12:01:05 +12:00
{
2016-08-27 12:28:05 +12:00
CreatePortable(PortableOutputDir, ReleaseDir);
2016-08-26 12:01:05 +12:00
}
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.CreateSteamFolder))
{
CreateSteamFolder();
}
if (Job.HasFlag(SetupJobs.CreatePortableAppsFolder))
{
CreatePortable(PortableAppsOutputDir, ReleaseDir);
}
if (Job.HasFlag(SetupJobs.OpenOutputDirectory))
{
OpenOutputDirectory();
}
2015-09-06 21:32:34 +12:00
2016-08-27 12:28:05 +12:00
if (Job.HasFlag(SetupJobs.UploadOutputFile))
{
2016-08-27 12:28:05 +12:00
UploadLatestFile();
}
2016-08-27 12:28:05 +12:00
Console.WriteLine("ShareX setup successfully completed.");
2016-08-26 12:01:05 +12:00
}
private static bool CheckArgs(string[] args, string check)
{
if (!string.IsNullOrEmpty(check))
{
foreach (string arg in args)
{
if (!string.IsNullOrEmpty(arg) && arg.Equals(check, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
}
return false;
}
2014-11-22 02:34:50 +13:00
private static void CompileSetup()
{
2016-08-27 12:28:05 +12:00
if (AppVeyor && !File.Exists(InnoSetupCompilerPath))
2016-08-26 12:01:05 +12:00
{
2016-08-26 15:41:34 +12:00
InstallInnoSetup();
2016-08-26 12:01:05 +12:00
}
2016-08-26 13:51:37 +12:00
if (File.Exists(InnoSetupCompilerPath))
2016-08-26 12:01:05 +12:00
{
2016-08-26 15:41:34 +12:00
CompileISSFile("Recorder-devices-setup.iss");
CompileISSFile("ShareX-setup.iss");
2016-08-26 12:01:05 +12:00
}
else
{
2016-08-26 13:51:37 +12:00
Console.WriteLine("InnoSetup compiler is missing: " + InnoSetupCompilerPath);
2016-08-26 12:01:05 +12:00
}
}
2016-08-26 15:41:34 +12:00
private static void InstallInnoSetup()
{
Console.WriteLine("Downloading InnoSetup.");
string innoSetupURL = "http://files.jrsoftware.org/is/5/innosetup-5.5.9-unicode.exe";
string innoSetupFilename = "innosetup-5.5.9-unicode.exe";
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(innoSetupURL, innoSetupFilename);
}
Console.WriteLine("Installing InnoSetup.");
Process.Start(innoSetupFilename, "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-").WaitForExit();
Console.WriteLine("InnoSetup installed.");
}
private static void CompileISSFile(string filename)
{
Console.WriteLine("Compiling setup file: " + filename);
ProcessStartInfo startInfo = new ProcessStartInfo(InnoSetupCompilerPath, $"\"{filename}\"");
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = Path.GetFullPath(InnoSetupDir);
Process process = Process.Start(startInfo);
2016-08-26 15:41:34 +12:00
process.WaitForExit();
Console.WriteLine("Setup file is created.");
}
2015-09-06 21:32:34 +12:00
private static void CreateSteamFolder()
{
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
2016-08-26 13:51:37 +12:00
CopyFile(Path.Combine(SteamLauncherDir, "ShareX_Launcher.exe"), SteamOutputDir);
CopyFile(Path.Combine(SteamLauncherDir, "steam_appid.txt"), SteamOutputDir);
CopyFile(Path.Combine(SteamLauncherDir, "installscript.vdf"), SteamOutputDir);
CopyFiles(SteamLauncherDir, "*.dll", SteamOutputDir);
2015-09-06 21:32:34 +12:00
CreatePortable(SteamUpdatesDir, SteamDir);
2015-09-06 21:32:34 +12:00
}
private static void CreatePortable(string destination, string releaseDirectory)
{
Console.WriteLine("Creating portable: " + destination);
2015-09-06 21:32:34 +12:00
if (Directory.Exists(destination))
2014-11-14 12:28:07 +13:00
{
2015-09-06 21:32:34 +12:00
Directory.Delete(destination, true);
2014-11-14 12:28:07 +13:00
}
2015-09-06 21:32:34 +12:00
Directory.CreateDirectory(destination);
2014-11-14 12:28:07 +13:00
CopyFile(Path.Combine(releaseDirectory, "ShareX.exe"), destination);
CopyFile(Path.Combine(releaseDirectory, "ShareX.exe.config"), destination);
CopyFiles(releaseDirectory, "*.dll", destination);
2016-08-26 12:10:09 +12:00
CopyFiles(Path.Combine(ParentDir, "Licenses"), "*.txt", Path.Combine(destination, "Licenses"));
CopyFile(Path.Combine(OutputDir, "Recorder-devices-setup.exe"), destination);
2016-08-27 12:02:43 +12:00
CopyFile(Path.Combine(ChromeDir, "ShareX_Chrome.exe"), destination);
2015-10-27 15:03:24 +13:00
string[] languages = new string[] { "de", "es", "fr", "hu", "ko-KR", "nl-NL", "pt-BR", "ru", "tr", "vi-VN", "zh-CN" };
2015-01-05 05:53:04 +13:00
foreach (string language in languages)
{
CopyFiles(Path.Combine(releaseDirectory, language), "*.resources.dll", Path.Combine(destination, "Languages", language));
2015-01-05 05:53:04 +13:00
}
if (destination.Equals(SteamUpdatesDir, StringComparison.InvariantCultureIgnoreCase))
{
// These git ignored
2016-08-26 12:10:09 +12:00
CopyFile(Path.Combine(ParentDir, "Lib", "ffmpeg.exe"), destination);
CopyFile(Path.Combine(ParentDir, "Lib", "ffmpeg-x64.exe"), destination);
}
2016-08-27 12:02:43 +12:00
else if (destination.Equals(PortableAppsOutputDir, StringComparison.InvariantCultureIgnoreCase))
{
File.Create(Path.Combine(destination, "PortableApps")).Dispose();
}
2015-09-28 01:46:29 +13:00
else
2015-09-06 21:32:34 +12:00
{
2015-10-27 15:03:24 +13:00
File.Create(Path.Combine(destination, "Portable")).Dispose();
2015-09-06 21:32:34 +12:00
//FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(releaseDir, "ShareX.exe"));
//string zipFilename = string.Format("ShareX-{0}.{1}.{2}-portable.zip", versionInfo.ProductMajorPart, versionInfo.ProductMinorPart, versionInfo.ProductBuildPart);
2016-08-26 13:51:37 +12:00
string zipPath = Path.Combine(OutputDir, "ShareX-portable.zip");
2014-01-13 05:05:31 +13:00
2015-09-06 21:32:34 +12:00
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
2014-01-13 05:05:31 +13:00
2015-11-22 13:45:26 +13:00
Zip(destination + "\\*", zipPath);
2014-01-13 05:05:31 +13:00
2015-09-06 21:32:34 +12:00
if (Directory.Exists(destination))
{
Directory.Delete(destination, true);
}
2014-01-16 08:12:59 +13:00
}
Console.WriteLine("Portable created.");
}
2014-01-13 05:05:31 +13:00
private static void OpenOutputDirectory()
{
2016-08-27 06:34:35 +12:00
Process.Start(OutputDir);
}
private static void UploadLatestFile()
{
FileInfo fileInfo = new DirectoryInfo(OutputDir).GetFiles("*.exe").OrderByDescending(f => f.LastWriteTime).FirstOrDefault();
2016-08-27 06:34:35 +12:00
if (fileInfo != null)
{
Console.WriteLine("Uploading setup file.");
2016-08-27 12:02:43 +12:00
Process.Start(DebugExecutablePath, fileInfo.FullName);
}
}
2015-09-28 01:46:29 +13:00
private static void CopyFiles(string[] files, string toFolder)
2014-11-14 12:28:07 +13:00
{
2015-09-03 01:46:20 +12:00
if (!Directory.Exists(toFolder))
{
Directory.CreateDirectory(toFolder);
}
2014-11-14 12:28:07 +13:00
foreach (string filepath in files)
{
string filename = Path.GetFileName(filepath);
string dest = Path.Combine(toFolder, filename);
File.Copy(filepath, dest);
}
}
2015-09-03 01:46:20 +12:00
private static void CopyFile(string path, string toFolder)
{
2015-09-05 18:51:38 +12:00
CopyFiles(new string[] { path }, toFolder);
2015-09-03 01:46:20 +12:00
}
private static void CopyFiles(string directory, string searchPattern, string toFolder)
2014-11-14 12:28:07 +13:00
{
2015-09-03 01:46:20 +12:00
CopyFiles(Directory.GetFiles(directory, searchPattern), toFolder);
2014-11-14 12:28:07 +13:00
}
2014-01-13 05:05:31 +13:00
private static void Zip(string source, string target)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ZipPath;
startInfo.Arguments = string.Format("a -tzip \"{0}\" \"{1}\" -r -mx=9", target, source);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(startInfo);
2014-01-13 05:05:31 +13:00
process.WaitForExit();
}
}
}