|
| 1 | +#!/usr/bin/env tclsh |
| 2 | +# Producer, a tool for creating Starpacks out of Git repositories. |
| 3 | +# Copyright (C) 2015 Danyil Bohdan. |
| 4 | +# License: MIT |
| 5 | + |
| 6 | +namespace eval ::packer {} |
| 7 | + |
| 8 | +proc ::packer::init {} { |
| 9 | + variable exampleBuildOptions [::packer::sl { |
| 10 | + # Where Packer's files are located. |
| 11 | + packerPath [pwd] |
| 12 | + |
| 13 | + # The path to the temporary directory in which the Starpacks are built. |
| 14 | + # Can be relative (to packerPath) or absolute. |
| 15 | + buildPath build |
| 16 | + |
| 17 | + # The path where to place the resulting Starpacks. |
| 18 | + # Can be relative (to packerPath) or absolute. |
| 19 | + artifactsPath artifacts |
| 20 | + |
| 21 | + # The Tclkit to run SDX with. |
| 22 | + buildTclkit {tclkit-8.6.3-rhel5-x86_64} |
| 23 | + |
| 24 | + # The Tclkit to use as runtime in the Starpack. |
| 25 | + targetTclkit {tclkit-8.6.3-rhel5-x86_64} |
| 26 | + |
| 27 | + # SDX Starkit. |
| 28 | + sdx {sdx-20110317.kit} |
| 29 | + |
| 30 | + # Tcllib archive. |
| 31 | + tcllib {Tcllib-1.16.tar.gz} |
| 32 | + |
| 33 | + # The Git repository to clone. Can be local or remote. |
| 34 | + sourceRepository {https://github.com/tclssg/tclssg} |
| 35 | + |
| 36 | + # The directory |
| 37 | + projectDir {tclssg} |
| 38 | + |
| 39 | + # |
| 40 | + targetFilename "tclssg.kit" |
| 41 | + |
| 42 | + # Whether to append the targetTclkit's file extension (e.g., ".exe") |
| 43 | + # to the file name. |
| 44 | + appendTclkitExtension 1 |
| 45 | + |
| 46 | + # Which file within the projectDir the Starpack should run on start. |
| 47 | + fileToRun {ssg.tcl} |
| 48 | + |
| 49 | + # Command line options to give to the Starpack to . Unset to not test. |
| 50 | + # Obviously, this work across architectures. |
| 51 | + testCommand {version} |
| 52 | + |
| 53 | + # The string to append to the targetFilename before the extension. If |
| 54 | + # not set everything after the first dash in the targetTclkit's rootname |
| 55 | + # is used. E.g, if targetTclkit is "tclssg-8.6.3-win32.kit.exe" the |
| 56 | + # default suffix will be "-8.6.3-win32.kit" |
| 57 | + # suffix {} |
| 58 | + }] |
| 59 | +} |
| 60 | + |
| 61 | +proc ::packer::build args { |
| 62 | + # Parse command line arguments. |
| 63 | + dict for {key value} $args { |
| 64 | + puts [format {%23s: %s} $key $value] |
| 65 | + set $key $value |
| 66 | + } |
| 67 | + |
| 68 | + # Defaults and mutation ahead. |
| 69 | + set buildPath [file join $packerPath $buildPath] |
| 70 | + set artifactsPath [file join $packerPath $artifactsPath] |
| 71 | + if {![info exists suffix]} { |
| 72 | + set suffix -[join [lrange [split \ |
| 73 | + [file rootname $targetTclkit] -] 1 end] -] |
| 74 | + } |
| 75 | + |
| 76 | + # Define procs for running external commands. |
| 77 | + foreach {procName command} [list \ |
| 78 | + git git \ |
| 79 | + tar tar \ |
| 80 | + tclkit [file join . $buildTclkit] \ |
| 81 | + ] { |
| 82 | + proc ::packer::$procName args [list apply {{command} { |
| 83 | + upvar 1 args args |
| 84 | + exec -ignorestderr -- {*}$command {*}$args |
| 85 | + }} $command] |
| 86 | + } |
| 87 | + |
| 88 | + # Build start. |
| 89 | + file delete -force $buildPath |
| 90 | + file mkdir $buildPath |
| 91 | + |
| 92 | + with-path $buildPath { |
| 93 | + git clone $sourceRepository |
| 94 | + |
| 95 | + foreach file [list $buildTclkit $targetTclkit $sdx $tcllib] { |
| 96 | + file copy -force [file join $packerPath $file] . |
| 97 | + } |
| 98 | + file attributes $buildTclkit -permissions +x |
| 99 | + |
| 100 | + file rename $projectDir "${projectDir}.vfs" |
| 101 | + |
| 102 | + # Create the file main.tcl to start $fileToRun. |
| 103 | + write-file [file join "${projectDir}.vfs" main.tcl] [list \ |
| 104 | + apply {{fileToRun} { |
| 105 | + global argv |
| 106 | + global argv0 |
| 107 | + package require starkit |
| 108 | + if {[starkit::startup] ne "sourced"} { |
| 109 | + source [file join $starkit::topdir $fileToRun] |
| 110 | + } |
| 111 | + }} $fileToRun |
| 112 | + ] |
| 113 | + |
| 114 | + # Unpack Tcllib and install it in lib/tcllib subdirectory of the Starkit |
| 115 | + # VFS. |
| 116 | + tar zxvf $tcllib |
| 117 | + |
| 118 | + with-path [regsub {.tar.gz$} $tcllib {}] { |
| 119 | + puts [exec -- \ |
| 120 | + tclsh ./installer.tcl -no-wait -no-gui -no-html -no-nroff \ |
| 121 | + -no-examples -no-apps -pkgs -pkg-path \ |
| 122 | + [file join $buildPath "${projectDir}.vfs" lib tcllib]] |
| 123 | + } |
| 124 | + |
| 125 | + # Wrap the Starpack. |
| 126 | + file copy $targetTclkit "${targetTclkit}.temp" |
| 127 | + tclkit $sdx wrap $targetFilename -runtime "${targetTclkit}.temp" |
| 128 | + file delete "${targetTclkit}.temp" |
| 129 | + |
| 130 | + # Run the test command. |
| 131 | + if {[info exists testCommand]} { |
| 132 | + file attributes $targetFilename -permissions +x |
| 133 | + puts [exec -- [file join . $targetFilename] {*}$testCommand] |
| 134 | + } |
| 135 | + |
| 136 | + # Store the build artifact. |
| 137 | + file mkdir $artifactsPath |
| 138 | + set artifactFilename \ |
| 139 | + [add-suffix-before-extension $targetFilename $suffix] |
| 140 | + if {[info exists appendTclkitExtension] && $appendTclkitExtension} { |
| 141 | + append artifactFilename [file extension $targetTclkit] |
| 142 | + } |
| 143 | + file copy -force $targetFilename \ |
| 144 | + [file join $artifactsPath $artifactFilename] |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +proc ::packer::read-file {fname {binary 0}} { |
| 149 | + set fpvar [open $fname r] |
| 150 | + if {$binary} { |
| 151 | + fconfigure $fpvar -translation binary |
| 152 | + } |
| 153 | + set content [read $fpvar] |
| 154 | + close $fpvar |
| 155 | + return $content |
| 156 | +} |
| 157 | + |
| 158 | +proc ::packer::write-file {fname content {binary 0}} { |
| 159 | + set fpvar [open $fname w] |
| 160 | + if {$binary} { |
| 161 | + fconfigure $fpvar -translation binary |
| 162 | + } |
| 163 | + puts -nonewline $fpvar $content |
| 164 | + close $fpvar |
| 165 | +} |
| 166 | + |
| 167 | +proc ::packer::with-path {path code} { |
| 168 | + set prevPath [pwd] |
| 169 | + cd $path |
| 170 | + uplevel 1 $code |
| 171 | + cd $prevPath |
| 172 | +} |
| 173 | + |
| 174 | +proc ::packer::add-suffix-before-extension {filename suffix} { |
| 175 | + return "[file rootname $filename]$suffix[file extension $filename]" |
| 176 | +} |
| 177 | + |
| 178 | +proc ::packer::sl script { |
| 179 | + # By Poor Yorick. From http://wiki.tcl.tk/39972. |
| 180 | + set res {} |
| 181 | + set parts {} |
| 182 | + foreach part [split $script \n] { |
| 183 | + lappend parts $part |
| 184 | + set part [join $parts \n] |
| 185 | + #add the newline that was stripped because it can make a difference |
| 186 | + if {[info complete $part\n]} { |
| 187 | + set parts {} |
| 188 | + set part [string trim $part] |
| 189 | + if {$part eq {}} { |
| 190 | + continue |
| 191 | + } |
| 192 | + if {[string index $part 0] eq {#}} { |
| 193 | + continue |
| 194 | + } |
| 195 | + #Here, the double-substitution via uplevel is intended! |
| 196 | + lappend res {*}[uplevel list $part] |
| 197 | + } |
| 198 | + } |
| 199 | + if {$parts ne {}} { |
| 200 | + error [list {incomplete parts} [join $parts]] |
| 201 | + } |
| 202 | + return $res |
| 203 | +} |
| 204 | + |
| 205 | +::packer::init |
0 commit comments