@matheusbsbs / matheus-bagliano / commits / 51a41ac4ef
Fork 0

git-gui: Use proper Windows shortcuts instead of bat files

On Windows its better to use a shortcut (.lnk file) over a batch script (.bat) as we can specify the icon file for the .lnk and thus have these git specific objects appear on the desktop with that git specific icon file. Unfortunately the authors of Tcl did not bless us with the APIs needed to create shortcuts from within Tcl. But Microsoft did give us Windows Scripting Host which allows us to execute some JavaScript that calls some sort of COM object that can operate on a .lnk file. We now build both Cygwin and non-Cygwin "desktop icons" as proper Windows .lnk files, using the "Start in" property of these files to indicate the working directory of the repository the user wants to launch. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Oct 12, 2007 at 16:11 UTC 51a41ac4efd8bcbcf2aa6e738c42ae4d46d10947
4 files changed +84 -35
Makefile
+6 -4
@@ -19,6 +19,10 @@ GITGUI_MAIN := git-gui
19 GITGUI_BUILT_INS = git-citool
20 ALL_LIBFILES = $(wildcard lib/*.tcl)
21 PRELOAD_FILES = lib/class.tcl
22 +NONTCL_LIBFILES = \
23 + lib/git-gui.ico \
24 + $(wildcard lib/win32_*.js) \
25 +#end NONTCL_LIBFILES
26
27 ifndef SHELL_PATH
28 SHELL_PATH = /bin/sh
@@ -255,12 +259,11 @@ ifdef GITGUI_WINDOWS_WRAPPER
259 endif
260 $(QUIET)$(INSTALL_D0)'$(DESTDIR_SQ)$(libdir_SQ)' $(INSTALL_D1)
261 $(QUIET)$(INSTALL_R0)lib/tclIndex $(INSTALL_R1) '$(DESTDIR_SQ)$(libdir_SQ)'
258 - $(QUIET)$(INSTALL_R0)lib/git-gui.ico $(INSTALL_R1) '$(DESTDIR_SQ)$(libdir_SQ)'
262 ifdef GITGUI_MACOSXAPP
263 $(QUIET)$(INSTALL_A0)'Git Gui.app' $(INSTALL_A1) '$(DESTDIR_SQ)$(libdir_SQ)'
264 $(QUIET)$(INSTALL_X0)git-gui.tcl $(INSTALL_X1) '$(DESTDIR_SQ)$(libdir_SQ)'
265 endif
263 - $(QUIET)$(foreach p,$(ALL_LIBFILES), $(INSTALL_R0)$p $(INSTALL_R1) '$(DESTDIR_SQ)$(libdir_SQ)' &&) true
266 + $(QUIET)$(foreach p,$(ALL_LIBFILES) $(NONTCL_LIBFILES), $(INSTALL_R0)$p $(INSTALL_R1) '$(DESTDIR_SQ)$(libdir_SQ)' &&) true
267 $(QUIET)$(INSTALL_D0)'$(DESTDIR_SQ)$(msgsdir_SQ)' $(INSTALL_D1)
268 $(QUIET)$(foreach p,$(ALL_MSGFILES), $(INSTALL_R0)$p $(INSTALL_R1) '$(DESTDIR_SQ)$(msgsdir_SQ)' &&) true
269
@@ -273,12 +276,11 @@ ifdef GITGUI_WINDOWS_WRAPPER
276 endif
277 $(QUIET)$(CLEAN_DST) '$(DESTDIR_SQ)$(libdir_SQ)'
278 $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(libdir_SQ)'/tclIndex $(REMOVE_F1)
276 - $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(libdir_SQ)'/git-gui.ico $(REMOVE_F1)
279 ifdef GITGUI_MACOSXAPP
280 $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(libdir_SQ)/Git Gui.app' $(REMOVE_F1)
281 $(QUIET)$(REMOVE_F0)'$(DESTDIR_SQ)$(libdir_SQ)'/git-gui.tcl $(REMOVE_F1)
282 endif
281 - $(QUIET)$(foreach p,$(ALL_LIBFILES), $(REMOVE_F0)'$(DESTDIR_SQ)$(libdir_SQ)'/$(notdir $p) $(REMOVE_F1) &&) true
283 + $(QUIET)$(foreach p,$(ALL_LIBFILES) $(NONTCL_LIBFILES), $(REMOVE_F0)'$(DESTDIR_SQ)$(libdir_SQ)'/$(notdir $p) $(REMOVE_F1) &&) true
284 $(QUIET)$(CLEAN_DST) '$(DESTDIR_SQ)$(msgsdir_SQ)'
285 $(QUIET)$(foreach p,$(ALL_MSGFILES), $(REMOVE_F0)'$(DESTDIR_SQ)$(msgsdir_SQ)'/$(notdir $p) $(REMOVE_F1) &&) true
286 $(QUIET)$(REMOVE_D0)'$(DESTDIR_SQ)$(gitexecdir_SQ)' $(REMOVE_D1)
lib/shortcut.tcl
+18 -31
@@ -2,28 +2,22 @@
2 # Copyright (C) 2006, 2007 Shawn Pearce
3
4 proc do_windows_shortcut {} {
5 - global argv0
6 -
5 set fn [tk_getSaveFile \
6 -parent . \
7 -title [append "[appname] ([reponame]): " [mc "Create Desktop Icon"]] \
10 - -initialfile "Git [reponame].bat"]
8 + -initialfile "Git [reponame].lnk"]
9 if {$fn != {}} {
12 - if {[file extension $fn] ne {.bat}} {
13 - set fn ${fn}.bat
10 + if {[file extension $fn] ne {.lnk}} {
11 + set fn ${fn}.lnk
12 }
13 if {[catch {
16 - set ge [file normalize [file dirname $::_git]]
17 - set fd [open $fn w]
18 - puts $fd "@ECHO Entering [reponame]"
19 - puts $fd "@ECHO Starting git-gui... please wait..."
20 - puts $fd "@SET PATH=$ge;%PATH%"
21 - puts $fd "@SET GIT_DIR=[file normalize [gitdir]]"
22 - puts -nonewline $fd "@\"[info nameofexecutable]\""
23 - puts $fd " \"[file normalize $argv0]\""
24 - close $fd
14 + win32_create_lnk $fn [list \
15 + [info nameofexecutable] \
16 + [file normalize $::argv0] \
17 + ] \
18 + [file dirname [file normalize [gitdir]]]
19 } err]} {
26 - error_popup [strcat [mc "Cannot write script:"] "\n\n$err"]
20 + error_popup [strcat [mc "Cannot write shortcut:"] "\n\n$err"]
21 }
22 }
23 }
@@ -44,13 +38,12 @@ proc do_cygwin_shortcut {} {
38 -parent . \
39 -title [append "[appname] ([reponame]): " [mc "Create Desktop Icon"]] \
40 -initialdir $desktop \
47 - -initialfile "Git [reponame].bat"]
41 + -initialfile "Git [reponame].lnk"]
42 if {$fn != {}} {
49 - if {[file extension $fn] ne {.bat}} {
50 - set fn ${fn}.bat
43 + if {[file extension $fn] ne {.lnk}} {
44 + set fn ${fn}.lnk
45 }
46 if {[catch {
53 - set fd [open $fn w]
47 set sh [exec cygpath \
48 --windows \
49 --absolute \
@@ -59,19 +52,13 @@ proc do_cygwin_shortcut {} {
52 --unix \
53 --absolute \
54 $argv0]
62 - set gd [exec cygpath \
63 - --unix \
64 - --absolute \
65 - [gitdir]]
66 - puts $fd "@ECHO Entering [reponame]"
67 - puts $fd "@ECHO Starting git-gui... please wait..."
68 - puts -nonewline $fd "@\"$sh\" --login -c \""
69 - puts -nonewline $fd "GIT_DIR=[sq $gd]"
70 - puts -nonewline $fd " [sq $me]"
71 - puts $fd " &\""
72 - close $fd
55 + win32_create_lnk $fn [list \
56 + $sh -c \
57 + "CHERE_INVOKING=1 source /etc/profile;[sq $me]" \
58 + ] \
59 + [file dirname [file normalize [gitdir]]]
60 } err]} {
74 - error_popup [strcat [mc "Cannot write script:"] "\n\n$err"]
61 + error_popup [strcat [mc "Cannot write shortcut:"] "\n\n$err"]
62 }
63 }
64 }
lib/win32.tcl new
+26
@@ -0,0 +1,26 @@
1 +# git-gui Misc. native Windows 32 support
2 +# Copyright (C) 2007 Shawn Pearce
3 +
4 +proc win32_read_lnk {lnk_path} {
5 + return [exec cscript.exe \
6 + /E:jscript \
7 + /nologo \
8 + [file join $::oguilib win32_shortcut.js] \
9 + $lnk_path]
10 +}
11 +
12 +proc win32_create_lnk {lnk_path lnk_exec lnk_dir} {
13 + global oguilib
14 +
15 + set lnk_args [lrange $lnk_exec 1 end]
16 + set lnk_exec [lindex $lnk_exec 0]
17 +
18 + eval [list exec wscript.exe \
19 + /E:jscript \
20 + /nologo \
21 + [file join $oguilib win32_shortcut.js] \
22 + $lnk_path \
23 + [file join $oguilib git-gui.ico] \
24 + $lnk_dir \
25 + $lnk_exec] $lnk_args
26 +}
lib/win32_shortcut.js new
+34
@@ -0,0 +1,34 @@
1 +// git-gui Windows shortcut support
2 +// Copyright (C) 2007 Shawn Pearce
3 +
4 +var WshShell = WScript.CreateObject("WScript.Shell");
5 +var argv = WScript.Arguments;
6 +var argi = 0;
7 +var lnk_path = argv.item(argi++);
8 +var ico_path = argi < argv.length ? argv.item(argi++) : undefined;
9 +var dir_path = argi < argv.length ? argv.item(argi++) : undefined;
10 +var lnk_exec = argi < argv.length ? argv.item(argi++) : undefined;
11 +var lnk_args = '';
12 +while (argi < argv.length) {
13 + var s = argv.item(argi++);
14 + if (lnk_args != '')
15 + lnk_args += ' ';
16 + if (s.indexOf(' ') >= 0) {
17 + lnk_args += '"';
18 + lnk_args += s;
19 + lnk_args += '"';
20 + } else {
21 + lnk_args += s;
22 + }
23 +}
24 +
25 +var lnk = WshShell.CreateShortcut(lnk_path);
26 +if (argv.length == 1) {
27 + WScript.echo(lnk.TargetPath);
28 +} else {
29 + lnk.TargetPath = lnk_exec;
30 + lnk.Arguments = lnk_args;
31 + lnk.IconLocation = ico_path + ", 0";
32 + lnk.WorkingDirectory = dir_path;
33 + lnk.Save();
34 +}