Rakefile 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. 'd3dcompiler_47.dll', 'libEGL.dll', 'libGLESv2.dll', 'swiftshader/libEGL.dll', 'swiftshader/libGLESv2.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(File.join(arch_config.installer_dir, 'vc++')) do
  163. FileList['*.dll'].each do |file|
  164. cp_to_portable(portable_dir, file)
  165. end
  166. end
  167. Dir.chdir(arch_config.installer_dir) do
  168. cp_to_portable(portable_dir, arch_config.syncthing_binaries[PORTABLE_SYNCTHING_VERSION], 'syncthing.exe')
  169. end
  170. sh %Q{"#{SZIP}" a -tzip -mx=7 -y #{arch_config.portable_output_file} #{portable_dir}}
  171. end
  172. end
  173. end
  174. end
  175. def create_checksums(checksum_file, password, algorithm, files)
  176. rm checksum_file if File.exist?(checksum_file)
  177. args = %Q{create "#{checksum_file}" #{algorithm} "#{CHECKSUM_FILE_PRIV_KEY}" "#{password}" } + files.map{ |x| "\"#{x}\"" }.join(' ')
  178. # Don't want to print out the pasword!
  179. puts "Invoking #{CHECKSUM_UTIL_EXE} for #{checksum_file}"
  180. system %Q{"#{CHECKSUM_UTIL_EXE}" #{args}}
  181. end
  182. desc 'Create checksums files'
  183. task :"create-checksums" => [:"build-checksum-util"] do
  184. password = ENV['PASSWORD'] || '""'
  185. checksum_file = File.join(DEPLOY_DIR, 'sha1sum.txt.asc')
  186. files = Dir["#{DEPLOY_DIR}/*.{zip,exe}"]
  187. create_checksums(File.join(DEPLOY_DIR, 'sha1sum.txt.asc'), password, 'sha1', files)
  188. create_checksums(File.join(DEPLOY_DIR, 'sha512sum.txt.asc'), password, 'sha512', files)
  189. end
  190. desc 'Clean portable and installer, all architectures'
  191. task :clean do
  192. rm_rf DEPLOY_DIR if File.exist?(DEPLOY_DIR)
  193. end
  194. namespace :package do
  195. ARCH_CONFIG.each do |arch_config|
  196. desc "Build installer and portable (#{arch_config.arch})"
  197. task arch_config.arch =>
  198. [
  199. :"update-syncthing:#{arch_config.arch}",
  200. :"build:#{arch_config.arch}",
  201. :"installer:#{arch_config.arch}",
  202. :"sign-installer:#{arch_config.arch}",
  203. :"portable:#{arch_config.arch}"
  204. ]
  205. end
  206. end
  207. desc 'Build installer and portable for all architectures'
  208. task :package => [:clean, *ARCH_CONFIG.map{ |x| :"package:#{x.arch}" }, :"create-checksums"]
  209. desc 'Build chocolatey package'
  210. task :chocolatey do
  211. chocolatey_dir = File.dirname(CHOCOLATEY_NUSPEC)
  212. cp Dir[File.join(DEPLOY_DIR, 'SyncTrayzorSetup-*.exe')], File.join(chocolatey_dir, 'tools')
  213. cp 'LICENSE.txt', File.join(chocolatey_dir, 'tools')
  214. Dir.chdir(chocolatey_dir) do
  215. sh "choco pack"
  216. end
  217. mv Dir[File.join(chocolatey_dir, 'synctrayzor.*.nupkg')], DEPLOY_DIR
  218. end
  219. desc "Bump version number"
  220. task :version, [:version] do |t, args|
  221. parts = args[:version].split('.')
  222. parts4 = parts.dup
  223. parts4 << '0' if parts4.length == 3
  224. version4 = parts4.join('.')
  225. ASSEMBLY_INFOS.each do |info|
  226. content = IO.read(info)
  227. content[/^\[assembly: AssemblyVersion\(\"(.+?)\"\)\]/, 1] = version4
  228. content[/^\[assembly: AssemblyFileVersion\(\"(.+?)\"\)\]/, 1] = version4
  229. File.open(info, 'w'){ |f| f.write(content) }
  230. end
  231. choco_content = IO.read(CHOCOLATEY_NUSPEC)
  232. choco_content[/<version>(.+?)<\/version>/, 1] = args[:version]
  233. File.open(CHOCOLATEY_NUSPEC, 'w'){ |f| f.write(choco_content) }
  234. end
  235. desc 'Create both 64-bit and 32-bit portable packages'
  236. task :portable => ARCH_CONFIG.map{ |x| :"portable:#{x.arch}" }
  237. namespace :syncthing do
  238. namespace :download do
  239. ARCH_CONFIG.each do |arch_config|
  240. desc "Download syncthing (#{arch_config.arch})"
  241. task arch_config.arch, [:version] => [:"build-checksum-util"] do |t, args|
  242. Dir.mktmpdir do |tmp|
  243. download_file = File.join(tmp, File.basename(arch_config.download_uri(args[:version])))
  244. File.open(download_file, 'wb') do |outfile|
  245. open(arch_config.download_uri(args[:version])) do |infile|
  246. outfile.write(infile.read)
  247. end
  248. end
  249. File.open(File.join(tmp, 'sha1sum.txt.asc.'), 'w') do |outfile|
  250. open(arch_config.sha1sum_download_uri(args[:version])) do |infile|
  251. outfile.write(infile.read)
  252. end
  253. end
  254. sh CHECKSUM_UTIL_EXE, 'verify', File.join(tmp, 'sha1sum.txt.asc'), 'sha1', SYNCTHING_RELEASES_CERT, download_file
  255. Dir.chdir(tmp) do
  256. sh %Q{"#{SZIP}" e -y #{File.basename(download_file)}}
  257. end
  258. cp File.join(tmp, 'syncthing.exe'), File.join(arch_config.installer_dir, 'syncthing.exe')
  259. end
  260. end
  261. end
  262. end
  263. desc 'Download syncthing for all architectures'
  264. task :download, [:version] => ARCH_CONFIG.map{ |x| :"syncthing:download:#{x.arch}" }
  265. namespace :update do
  266. ARCH_CONFIG.each do |arch_config|
  267. desc "Update syncthing binaries (#{arch_config.arch}"
  268. task arch_config.arch do
  269. arch_config.syncthing_binaries.values_at(*SYNCTHING_VERSIONS_TO_UPDATE).each do |bin|
  270. path = File.join(arch_config.installer_dir, bin)
  271. raise "Could not find #{path}" unless File.exist?(path)
  272. Dir.mktmpdir do |tmp|
  273. sh path, '-upgrade', "-home=#{tmp}" do; end
  274. end
  275. old_bin = "#{path}.old"
  276. rm old_bin if File.exist?(old_bin)
  277. end
  278. end
  279. end
  280. end
  281. desc 'Update syncthing binaries, all architectures'
  282. task :update => ARCH_CONFIG.map{ |x| :"syncthing:update:#{x.arch}" }
  283. end
  284. def create_tx_client
  285. raise "TX_PASSWORD not specified" if ENV['TX_PASSWORD'].nil? || ENV['TX_PASSWORD'].empty?
  286. TxClient.new('synctrayzor', 'strings', 'canton7', ENV['TX_PASSWORD'])
  287. end
  288. def create_csproj_resx_writer
  289. csproj_resx_writer = CsprojResxWriter.new('src/SyncTrayzor/SyncTrayzor.csproj', 'Properties')
  290. csproj_resx_writer.language_exceptions['es_ES'] = 'es'
  291. csproj_resx_writer
  292. end
  293. namespace :tx do
  294. desc "Remove all translations from csproj"
  295. task :clean do
  296. create_csproj_resx_writer().remove_all_resx
  297. puts "Cleaned translations"
  298. end
  299. desc "Fetch all translations"
  300. task :pull => [:"tx:clean"] do
  301. tx_client = create_tx_client()
  302. csproj_resx_writer = create_csproj_resx_writer()
  303. tx_client.list_translations().each do |language|
  304. next if language == 'en'
  305. puts "Fetching #{language}..."
  306. tx_client.download_translation(language, csproj_resx_writer.absolute_resx_path_for_language(language))
  307. csproj_resx_writer.add_resx_to_csproj(language)
  308. end
  309. end
  310. desc "Push source translations"
  311. task :push do
  312. tx_client = create_tx_client()
  313. csproj_resx_writer = create_csproj_resx_writer()
  314. source_resx = csproj_resx_writer.read_and_sort_source_resx
  315. response = tx_client.upload_source(source_resx)
  316. puts "Added: #{response['strings_added']}. Updated: #{response['strings_updated']}. Deleted: #{response['strings_delete']}."
  317. end
  318. end
  319. namespace :icons do
  320. desc "Create a tray icon (pass 256x256 source)"
  321. task :"tray-icon", [:source] do |t,args|
  322. source = args[:source]
  323. # See https://wiki.lazarus.freepascal.org/Windows_Icon
  324. sizes = [
  325. 16, 32, # Normal 96 DPI
  326. 20, 40, # 120 DPI (125%)
  327. 24, 48, # 144 DPI (150%)
  328. 32, 64, # 192 DPI (200%)
  329. 36, 72, # 225%
  330. 40, 80, # 250%
  331. 44, 88, # 275%
  332. 48, 96, # 300%
  333. ].uniq.sort
  334. raise "Need a source image" unless source
  335. Dir.chdir(File.join('src', 'SyncTrayzor', 'Icons')) do
  336. sizes.each do |size|
  337. sh 'magick', 'convert', source, '-resize', "#{size}x#{size}", source.pathmap("%n-#{size}%x")
  338. end
  339. sh 'magick', 'convert', *sizes.map{ |x| source.pathmap("%n-#{x}%x") }, source.pathmap('%n.ico')
  340. rm sizes.map{ |x| source.pathmap("%n-#{x}%x") }
  341. end
  342. end
  343. end