-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_app.bat
More file actions
149 lines (128 loc) · 4.64 KB
/
run_app.bat
File metadata and controls
149 lines (128 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
@echo off
setlocal EnableDelayedExpansion
:: ============================================================================
:: [EN] UNIVERSAL LAUNCHER
:: This script acts as a wrapper to run Python scripts within a specific
:: virtual environment.
::
:: USAGE:
:: 1. Drag and drop any .py or .pyw file onto this batch file.
:: 2. Or run it from the command line: run_app.bat <script_to_run>
::
:: AUTO-SETUP:
:: - Creates a virtual environment (.venv) if missing.
:: - Installs dependencies from 'requirements.txt' if present.
:: - Checks for an icon (.ico) in the folder to assign to the shortcut.
:: - Prompts to create a Desktop shortcut for the dragged script.
::
:: [FR] LANCEUR UNIVERSEL
:: Ce script sert d'enveloppe pour exécuter des scripts Python dans un
:: environnement virtuel spécifique.
::
:: UTILISATION :
:: 1. Glissez et déposez n'importe quel fichier .py ou .pyw sur ce fichier.
:: 2. Ou lancez-le depuis la ligne de commande : run_app.bat <script_a_lancer>
::
:: AUTO-CONFIG :
:: - Crée un environnement virtuel (.venv) s'il est absent.
:: - Installe les dépendances via 'requirements.txt' si présent.
:: - Cherche une icône (.ico) dans le dossier pour le raccourci.
:: - Propose de créer un raccourci Bureau pour le script déposé.
:: ============================================================================
set "VENV_NAME=.venv"
set "REQ_FILE=requirements.txt"
:: 1. TARGET FILE ANALYSIS
set "TARGET_SCRIPT=%~f1"
set "SCRIPT_NAME=%~nx1"
set "SCRIPT_NAME_NOEXT=%~n1"
set "FILE_EXT=%~x1"
set "LAUNCHER_PATH=%~f0"
title Launcher : !SCRIPT_NAME!
:: 2. INPUT VERIFICATION
if "!TARGET_SCRIPT!"=="" (
echo [ERROR] No file provided.
echo [INFO] Drag and drop a script to launch it.
pause
exit /b 1
)
:: 3. ENVIRONMENT SETUP
if not exist "%VENV_NAME%" (
echo [SETUP] Creating venv...
python -m venv "%VENV_NAME%"
)
if exist "%REQ_FILE%" (
"%VENV_NAME%\Scripts\python.exe" -m pip install --upgrade pip -q >nul 2>&1
"%VENV_NAME%\Scripts\python.exe" -m pip install -r "%REQ_FILE%" -q
)
:: --------------------------------------------------------
:: 4. ICON MANAGEMENT
:: --------------------------------------------------------
set "ICON_PATH=%SystemRoot%\System32\shell32.dll,238"
for %%I in ("%~dp0*.ico") do (
set "ICON_PATH=%%~fI"
goto :ICON_FOUND
)
:ICON_FOUND
:: --------------------------------------------------------
:: 5. SHORTCUT CREATION PROMPT (Yes by Default)
:: --------------------------------------------------------
set "SHORTCUT_PATH=%USERPROFILE%\Desktop\!SCRIPT_NAME_NOEXT!.lnk"
:: Only ask if the shortcut does NOT exist yet
if not exist "!SHORTCUT_PATH!" (
echo.
echo --------------------------------------------------------
:: UX TRICK: Default response is "Y"
set "USER_CHOICE=Y"
:: Prompt the user (User sees [Y/n])
set /p "USER_CHOICE=[QUESTION] Create a shortcut on the Desktop? [Y/n] : "
:: If user types 'n' or 'N', we skip
if /I "!USER_CHOICE!"=="N" (
echo [INFO] No shortcut created.
) else (
echo [INSTALLATION] Creating shortcut...
(
echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
echo sLinkFile = "!SHORTCUT_PATH!"
echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
echo oLink.TargetPath = "!LAUNCHER_PATH!"
echo oLink.Arguments = Chr^(34^) ^& "!TARGET_SCRIPT!" ^& Chr^(34^)
echo oLink.Description = "Launch !SCRIPT_NAME!"
echo oLink.IconLocation = "!ICON_PATH!"
echo oLink.WorkingDirectory = "%~dp0"
echo oLink.Save
) > "%TEMP%\CreateShortcut.vbs"
cscript //nologo "%TEMP%\CreateShortcut.vbs"
del "%TEMP%\CreateShortcut.vbs"
echo [SUCCESS] Shortcut created!
timeout /t 1 >nul
)
)
:: 6. THE CONTROLLER (GUI vs Console Mode)
if /I "!FILE_EXT!"==".pyw" (
goto MODE_GUI
) else (
goto MODE_CONSOLE
)
:: ========================================================
:MODE_GUI
:: ========================================================
start "" "%VENV_NAME%\Scripts\pythonw.exe" "!TARGET_SCRIPT!"
exit
:: ========================================================
:MODE_CONSOLE
:: ========================================================
echo.
echo [CONSOLE MODE] Launching !SCRIPT_NAME!
echo --------------------------------------------------------
"%VENV_NAME%\Scripts\python.exe" "!TARGET_SCRIPT!"
set EXIT_CODE=%errorlevel%
echo.
echo --------------------------------------------------------
if %EXIT_CODE% neq 0 (
echo [END] Stopped with ERROR (Code: %EXIT_CODE%)
) else (
echo [END] Execution finished.
)
echo --------------------------------------------------------
pause
exit