Rakefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. require 'tmpdir'
  2. require 'open-uri'
  3. require_relative 'build/TxClient'
  4. require_relative 'build/CsprojResxWriter'
  5. ISCC = ENV['ISCC'] || 'C:\Program Files (x86)\Inno Setup 6\ISCC.exe'
  6. SZIP = ENV['SZIP'] || File.join(__dir__, 'build', '7za.exe')
  7. SIGNTOOL = ENV['SIGNTOOL'] || 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin\signtool.exe'
  8. VSWHERE = 'build/vswhere.exe'
  9. CONFIG = ENV['CONFIG'] || 'Release'
  10. MSBUILD_LOGGER = ENV['MSBUILD_LOGGER']
  11. SRC_DIR = 'src/SyncTrayzor'
  12. INSTALLER_DIR = 'installer'
  13. DEPLOY_DIR = 'deploy'
  14. SLN = 'src/SyncTrayzor.sln'
  15. CHOCOLATEY_NUSPEC = 'chocolatey/synctrayzor.nuspec'
  16. CHECKSUM_UTIL_CSPROJ = 'src/ChecksumUtil/ChecksumUtil.csproj'
  17. CHECKSUM_UTIL_EXE = 'bin/ChecksumUtil/Release/ChecksumUtil.exe'
  18. SYNCTHING_RELEASES_CERT = 'security/syncthing_releases_cert.asc'
  19. CHECKSUM_FILE_PRIV_KEY = 'security/private_key.asc'
  20. PFX = ENV['PFX'] || File.join(INSTALLER_DIR, 'SyncTrayzorCA.pfx')
  21. PORTABLE_SYNCTHING_VERSION = 'latest'
  22. class ArchDirConfig
  23. attr_reader :arch
  24. attr_reader :bin_dir
  25. attr_reader :installer_dir
  26. attr_reader :installer_output
  27. attr_reader :installer_iss
  28. attr_reader :portable_output_dir
  29. attr_reader :portable_output_file
  30. attr_reader :syncthing_binaries
  31. def initialize(arch, github_arch)
  32. @arch = arch
  33. @github_arch = github_arch
  34. @bin_dir = "bin/#{@arch}/#{CONFIG}"
  35. @installer_dir = File.join(INSTALLER_DIR, @arch)
  36. @installer_output = File.join(@installer_dir, "SyncTrayzorSetup-#{@arch}.exe")
  37. @installer_iss = File.join(@installer_dir, "installer-#{@arch}.iss")
  38. @portable_output_dir = "SyncTrayzorPortable-#{@arch}"
  39. @portable_output_file = File.join(DEPLOY_DIR, "SyncTrayzorPortable-#{@arch}.zip")
  40. @syncthing_binaries = { 'latest' => 'syncthing.exe' }
  41. end
  42. def sha1sum_download_uri(version)
  43. "https://github.com/syncthing/syncthing/releases/download/v#{version}/sha1sum.txt.asc"
  44. end
  45. def download_uri(version)
  46. "https://github.com/syncthing/syncthing/releases/download/v#{version}/syncthing-windows-#{@github_arch}-v#{version}.zip"
  47. end
  48. end
  49. SYNCTHING_VERSIONS_TO_UPDATE = ['latest']
  50. ARCH_CONFIG = [ArchDirConfig.new('x64', 'amd64'), ArchDirConfig.new('x86', '386')]
  51. ASSEMBLY_INFOS = FileList['**/AssemblyInfo.cs']
  52. def build(sln, platform, rebuild = true)
  53. if ENV['MSBUILD']
  54. msbuild = ENV['MSBUILD']
  55. else
  56. path = `#{VSWHERE} -requires Microsoft.Component.MSBuild -format value -property installationPath`.chomp
  57. msbuild = File.join(path, 'MSBuild', 'Current', 'Bin', 'MSBuild.exe')
  58. end
  59. puts "MSBuild is at #{msbuild}"
  60. tasks = rebuild ? 'Clean;Rebuild' : 'Build'
  61. cmd = "\"#{msbuild}\" \"#{sln}\" -t:#{tasks} -p:Configuration=#{CONFIG};Platform=#{platform}"
  62. if MSBUILD_LOGGER
  63. cmd << " -logger:\"#{MSBUILD_LOGGER}\" /verbosity:minimal"
  64. else
  65. cmd << " -verbosity:quiet"
  66. end
  67. puts cmd
  68. sh cmd
  69. end
  70. namespace :build do
  71. ARCH_CONFIG.each do |arch_config|
  72. desc "Build the project (#{arch_config.arch})"
  73. task arch_config.arch do
  74. build(SLN, arch_config.arch)
  75. end
  76. end
  77. end
  78. desc 'Build both 64-bit and 32-bit binaries'
  79. task :build => ARCH_CONFIG.map{ |x| :"build:#{x.arch}" }
  80. task :"build-checksum-util" do
  81. build(CHECKSUM_UTIL_CSPROJ, 'AnyCPU', false)
  82. end
  83. namespace :installer do
  84. ARCH_CONFIG.each do |arch_config|
  85. desc "Create the installer (#{arch_config.arch})"
  86. task arch_config.arch do
  87. unless File.exist?(ISCC)
  88. warn "Please install Inno Setup"
  89. exit 1
  90. end
  91. rm arch_config.installer_output if File.exist?(arch_config.installer_output)
  92. sh %Q{"#{ISCC}" #{arch_config.installer_iss}}
  93. mkdir_p DEPLOY_DIR
  94. mv arch_config.installer_output, DEPLOY_DIR
  95. end
  96. end
  97. end
  98. desc 'Build both 64-bit and 32-bit installers'
  99. task :installer => ARCH_CONFIG.map{ |x| :"installer:#{x.arch}" }
  100. namespace :"sign-installer" do
  101. ARCH_CONFIG.each do |arch_config|
  102. desc "Sign the installer (#{arch_config.arch}). Specify PASSWORD if required"
  103. task arch_config.arch do
  104. unless File.exist?(SIGNTOOL)
  105. warn "You must install the Windows SDK"
  106. exit 1
  107. end
  108. unless File.exist?(PFX)
  109. warn "#{PFX} must exist"
  110. exit 1
  111. end
  112. args = "sign /f #{PFX} /t http://timestamp.verisign.com/scripts/timstamp.dll"
  113. args << " /p #{ENV['PASSWORD']}" if ENV['PASSWORD']
  114. args << " /v #{File.join(DEPLOY_DIR, File.basename(arch_config.installer_output))}"
  115. # Don't want to print out the pasword!
  116. puts "Invoking signtool"
  117. system %Q{"#{SIGNTOOL}" #{args}}
  118. end
  119. end
  120. end
  121. desc 'Sign both 64-bit and 32-bit installers. Specify PASSWORD if required'
  122. task :"sign-installer" => ARCH_CONFIG.map{ |x| :"sign-installer:#{x.arch}" }
  123. def cp_to_portable(output_dir, src, output_filename = nil)
  124. dest = File.join(output_dir, output_filename || src)
  125. raise "Cannot find #{src}" unless File.exist?(src)
  126. # It could be an empty directory - so ignore it
  127. # We'll create it as and when if there are any files in it
  128. if File.file?(src)
  129. mkdir_p File.dirname(dest) unless File.exist?(File.dirname(dest))
  130. cp src, dest
  131. end
  132. end
  133. namespace :portable do
  134. ARCH_CONFIG.each do |arch_config|
  135. desc "Create the portable package (#{arch_config.arch})"
  136. task arch_config.arch do
  137. mkdir_p File.dirname(arch_config.portable_output_file)
  138. rm arch_config.portable_output_file if File.exist?(arch_config.portable_output_file)
  139. Dir.mktmpdir do |tmp|
  140. portable_dir = File.join(tmp, arch_config.portable_output_dir)
  141. Dir.chdir(arch_config.bin_dir) do
  142. files = FileList['**/*'].exclude(
  143. '*.xml', '*.vshost.*', '*.log', '*.Installer.config', '*/FluentValidation.resources.dll',
  144. '*/System.Windows.Interactivity.resources.dll', 'syncthing.exe', 'data/*', 'logs',
  145. 'ffmpegsumo.dll', 'd3dcompiler_43.dll', 'd3dcompiler_47.dll', 'libEGL.dll', 'libGLESv2.dll', 'pdf.dll')
  146. files.each do |file|
  147. cp_to_portable(portable_dir, file)
  148. end
  149. end
  150. Dir.chdir(File.join('bin', 'PortableInstaller', CONFIG)) do
  151. cp_to_portable(portable_dir, 'PortableInstaller.exe')
  152. end
  153. cp File.join(SRC_DIR, 'Icons', 'default.ico'), arch_config.portable_output_dir
  154. FileList['*.md', '*.txt'].each do |file|
  155. cp_to_portable(portable_dir, file)
  156. end
  157. Dir.chdir(File.join(arch_config.installer_dir, 'ucrt')) do
  158. FileList['*.dll'].each do |file|
  159. cp_to_portable(portable_dir, file)
  160. end
  161. end
  162. Dir.chdir(arch_config.installer_dir) do
  163. cp_to_portable(portable_dir, arch_config.syncthing_binaries[PORTABLE_SYNCTHING_VERSION], 'syncthing.exe')
  164. end
  165. sh %Q{"#{SZIP}" a -tzip -mx=7 -y #{arch_config.portable_output_file} #{portable_dir}}
  166. end
  167. end
  168. end
  169. end
  170. def create_checksums(checksum_file, password, algorithm, files)
  171. rm checksum_file if File.exist?(checksum_file)
  172. args = %Q{create "#{checksum_file}" #{algorithm} "#{CHECKSUM_FILE_PRIV_KEY}" "#{password}" } + files.map{ |x| "\"#{x}\"" }.join(' ')
  173. # Don't want to print out the pasword!
  174. puts "Invoking #{CHECKSUM_UTIL_EXE} for #{checksum_file}"
  175. system %Q{"#{CHECKSUM_UTIL_EXE}" #{args}}
  176. end
  177. desc 'Create checksums files'
  178. task :"create-checksums" => [:"build-checksum-util"] do
  179. password = ENV['PASSWORD'] || '""'
  180. checksum_file = File.join(DEPLOY_DIR, 'sha1sum.txt.asc')
  181. files = Dir["#{DEPLOY_DIR}/*.{zip,exe}"]
  182. create_checksums(File.join(DEPLOY_DIR, 'sha1sum.txt.asc'), password, 'sha1', files)
  183. create_checksums(File.join(DEPLOY_DIR, 'sha512sum.txt.asc'), password, 'sha512', files)
  184. end
  185. desc 'Clean portable and installer, all architectures'
  186. task :clean do
  187. rm_rf DEPLOY_DIR if File.exist?(DEPLOY_DIR)
  188. end
  189. namespace :package do
  190. ARCH_CONFIG.each do |arch_config|
  191. desc "Build installer and portable (#{arch_config.arch})"
  192. task arch_config.arch =>
  193. [
  194. :"update-syncthing:#{arch_config.arch}",
  195. :"build:#{arch_config.arch}",
  196. :"installer:#{arch_config.arch}",
  197. :"sign-installer:#{arch_config.arch}",
  198. :"portable:#{arch_config.arch}"
  199. ]
  200. end
  201. end
  202. desc 'Build installer and portable for all architectures'
  203. task :package => [:clean, *ARCH_CONFIG.map{ |x| :"package:#{x.arch}" }, :"create-checksums"]
  204. desc 'Build chocolatey package'
  205. task :chocolatey do
  206. chocolatey_dir = File.dirname(CHOCOLATEY_NUSPEC)
  207. cp Dir[File.join(DEPLOY_DIR, 'SyncTrayzorSetup-*.exe')], File.join(chocolatey_dir, 'tools')
  208. cp 'LICENSE.txt', File.join(chocolatey_dir, 'tools')
  209. Dir.chdir(chocolatey_dir) do
  210. sh "choco pack"
  211. end
  212. mv Dir[File.join(chocolatey_dir, 'synctrayzor.*.nupkg')], DEPLOY_DIR
  213. end
  214. desc "Bump version number"
  215. task :version, [:version] do |t, args|
  216. parts = args[:version].split('.')
  217. parts4 = parts.dup
  218. parts4 << '0' if parts4.length == 3
  219. version4 = parts4.join('.')
  220. ASSEMBLY_INFOS.each do |info|
  221. content = IO.read(info)
  222. content[/^\[assembly: AssemblyVersion\(\"(.+?)\"\)\]/, 1] = version4
  223. content[/^\[assembly: AssemblyFileVersion\(\"(.+?)\"\)\]/, 1] = version4
  224. File.open(info, 'w'){ |f| f.write(content) }
  225. end
  226. choco_content = IO.read(CHOCOLATEY_NUSPEC)
  227. choco_content[/<version>(.+?)<\/version>/, 1] = args[:version]
  228. File.open(CHOCOLATEY_NUSPEC, 'w'){ |f| f.write(choco_content) }
  229. end
  230. desc 'Create both 64-bit and 32-bit portable packages'
  231. task :portable => ARCH_CONFIG.map{ |x| :"portable:#{x.arch}" }
  232. namespace :syncthing do
  233. namespace :download do
  234. ARCH_CONFIG.each do |arch_config|
  235. desc "Download syncthing (#{arch_config.arch})"
  236. task arch_config.arch, [:version] => [:"build-checksum-util"] do |t, args|
  237. Dir.mktmpdir do |tmp|
  238. download_file = File.join(tmp, File.basename(arch_config.download_uri(args[:version])))
  239. File.open(download_file, 'wb') do |outfile|
  240. open(arch_config.download_uri(args[:version])) do |infile|
  241. outfile.write(infile.read)
  242. end
  243. end
  244. File.open(File.join(tmp, 'sha1sum.txt.asc.'), 'w') do |outfile|
  245. open(arch_config.sha1sum_download_uri(args[:version])) do |infile|
  246. outfile.write(infile.read)
  247. end
  248. end
  249. sh CHECKSUM_UTIL_EXE, 'verify', File.join(tmp, 'sha1sum.txt.asc'), 'sha1', SYNCTHING_RELEASES_CERT, download_file
  250. Dir.chdir(tmp) do
  251. sh %Q{"#{SZIP}" e -y #{File.basename(download_file)}}
  252. end
  253. cp File.join(tmp, 'syncthing.exe'), File.join(arch_config.installer_dir, 'syncthing.exe')
  254. end
  255. end
  256. end
  257. end
  258. desc 'Download syncthing for all architectures'
  259. task :download, [:version] => ARCH_CONFIG.map{ |x| :"syncthing:download:#{x.arch}" }
  260. namespace :update do
  261. ARCH_CONFIG.each do |arch_config|
  262. desc "Update syncthing binaries (#{arch_config.arch}"
  263. task arch_config.arch do
  264. arch_config.syncthing_binaries.values_at(*SYNCTHING_VERSIONS_TO_UPDATE).each do |bin|
  265. path = File.join(arch_config.installer_dir, bin)
  266. raise "Could not find #{path}" unless File.exist?(path)
  267. Dir.mktmpdir do |tmp|
  268. sh path, '-upgrade', "-home=#{tmp}" do; end
  269. end
  270. old_bin = "#{path}.old"
  271. rm old_bin if File.exist?(old_bin)
  272. end
  273. end
  274. end
  275. end
  276. desc 'Update syncthing binaries, all architectures'
  277. task :update => ARCH_CONFIG.map{ |x| :"syncthing:update:#{x.arch}" }
  278. end
  279. def create_tx_client
  280. raise "TX_PASSWORD not specified" if ENV['TX_PASSWORD'].nil? || ENV['TX_PASSWORD'].empty?
  281. TxClient.new('synctrayzor', 'strings', 'canton7', ENV['TX_PASSWORD'])
  282. end
  283. def create_csproj_resx_writer
  284. csproj_resx_writer = CsprojResxWriter.new('src/SyncTrayzor/SyncTrayzor.csproj', 'Properties')
  285. csproj_resx_writer.language_exceptions['es_ES'] = 'es'
  286. csproj_resx_writer
  287. end
  288. namespace :tx do
  289. desc "Remove all translations from csproj"
  290. task :clean do
  291. create_csproj_resx_writer().remove_all_resx
  292. puts "Cleaned translations"
  293. end
  294. desc "Fetch all translations"
  295. task :pull => [:"tx:clean"] do
  296. tx_client = create_tx_client()
  297. csproj_resx_writer = create_csproj_resx_writer()
  298. tx_client.list_translations().each do |language|
  299. next if language == 'en'
  300. puts "Fetching #{language}..."
  301. tx_client.download_translation(language, csproj_resx_writer.absolute_resx_path_for_language(language))
  302. csproj_resx_writer.add_resx_to_csproj(language)
  303. end
  304. end
  305. desc "Push source translations"
  306. task :push do
  307. tx_client = create_tx_client()
  308. csproj_resx_writer = create_csproj_resx_writer()
  309. source_resx = csproj_resx_writer.read_and_sort_source_resx
  310. response = tx_client.upload_source(source_resx)
  311. puts "Added: #{response['strings_added']}. Updated: #{response['strings_updated']}. Deleted: #{response['strings_delete']}."
  312. end
  313. end