generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use internal venv for dependency management (#9)
* feat: use internal venv for dependency management * fix: setup script * chore: readme & scripts * chore: service provider * Fix styling * chore: progress * fix: process->start() * fix: formatting * chore: reduce verbosity * fix: --no-progress-bar not available * chore(ci): remove post-install command * chore: change post-update to setup-python * feat: more output * chore: docs * fix: install command * fix: remove unnecessary adds * chore: fix tests * fix: ci * chore(ci): change to default error format * chore(ci): ignore error in ci --------- Co-authored-by: kauffinger <[email protected]>
- Loading branch information
1 parent
79979af
commit d5e2867
Showing
20 changed files
with
480 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
markitdown==0.0.1a2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/bin/bash | ||
|
||
set -e # Exit on error | ||
|
||
# Function to log messages | ||
log() { | ||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | ||
} | ||
|
||
# Function to check command availability | ||
check_command() { | ||
if ! command -v "$1" &> /dev/null; then | ||
log "Error: $1 is required but not installed." | ||
exit 1 | ||
fi | ||
} | ||
|
||
# Check required commands | ||
check_command python3 | ||
check_command pip3 | ||
|
||
# Determine the directory of this script | ||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
|
||
# Path to the python directory within your package | ||
PYTHON_DIR="$DIR/python" | ||
|
||
# Create python directory if it doesn't exist | ||
if [ ! -d "$PYTHON_DIR" ]; then | ||
log "Creating Python directory..." | ||
mkdir -p "$PYTHON_DIR" | ||
fi | ||
|
||
# Navigate to the python directory | ||
cd "$PYTHON_DIR" || { | ||
log "Error: Failed to change to directory $PYTHON_DIR" | ||
exit 1 | ||
} | ||
|
||
# Check if the virtual environment already exists | ||
if [ ! -d "venv" ]; then | ||
log "Creating Python virtual environment..." | ||
python3 -m venv venv || { | ||
log "Error: Failed to create virtual environment" | ||
exit 1 | ||
} | ||
fi | ||
|
||
# Install Python dependencies | ||
log "Installing Python dependencies..." | ||
if [ -f "requirements.txt" ]; then | ||
# Capture both stdout and stderr | ||
if ! output=$(venv/bin/python -m pip install -r requirements.txt -q 2>&1); then | ||
log "Error: Failed to install dependencies" | ||
log "Pip output: $output" | ||
exit 1 | ||
fi | ||
else | ||
log "Warning: requirements.txt not found in $PYTHON_DIR" | ||
exit 1 | ||
fi | ||
|
||
log "Python environment setup complete." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Innobrain\Markitdown\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Process; | ||
|
||
class InstallCommand extends Command | ||
{ | ||
public $signature = 'markitdown:install'; | ||
|
||
public $description = 'Install Markitdown Python dependencies and set up the virtual environment'; | ||
|
||
public function handle(): int | ||
{ | ||
$this->components->info('Installing Markitdown...'); | ||
|
||
// Get the path to the user's composer.json | ||
$composerPath = $this->getLaravel()->basePath('composer.json'); | ||
|
||
if (! file_exists($composerPath)) { | ||
$this->components->error('composer.json not found in project root.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
// Read composer.json | ||
$composerJson = file_get_contents($composerPath); | ||
|
||
if ($composerJson === false) { | ||
$this->components->error('Failed to read composer.json.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
/** @var mixed $composer */ | ||
$composer = json_decode($composerJson, true); | ||
|
||
if (! is_array($composer)) { | ||
$this->components->error('Invalid composer.json format.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
/** @var array<string, mixed> $composer */ | ||
|
||
// Initialize scripts array if it doesn't exist | ||
if (! isset($composer['scripts'])) { | ||
$composer['scripts'] = []; | ||
} | ||
|
||
/** @var array<string, array<string, mixed>> $composer */ | ||
|
||
// Add our script to the project's composer.json scripts | ||
$scriptPath = './vendor/innobrain/markitdown/setup-python-env.sh'; | ||
$scriptAdded = false; | ||
|
||
// Add to post-autoload-dump | ||
if (! isset($composer['scripts']['post-autoload-dump'])) { | ||
$composer['scripts']['post-autoload-dump'] = []; | ||
} | ||
|
||
/** @var array<string, array<int|string, string|array<string>>> $composer */ | ||
|
||
// Ensure post-autoload-dump is an array | ||
if (! is_array($composer['scripts']['post-autoload-dump'])) { | ||
$composer['scripts']['post-autoload-dump'] = [$composer['scripts']['post-autoload-dump']]; | ||
} | ||
|
||
if (! in_array($scriptPath, $composer['scripts']['post-autoload-dump'], true)) { | ||
$composer['scripts']['post-autoload-dump'][] = $scriptPath; | ||
$scriptAdded = true; | ||
} | ||
|
||
if ($scriptAdded) { | ||
// Write back to composer.json with proper formatting | ||
$encodedJson = json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | ||
|
||
if ($encodedJson === false) { | ||
$this->components->error('Failed to encode composer.json.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$formattedJson = str($encodedJson) | ||
->append(PHP_EOL) | ||
->replace( | ||
search: " \"keywords\": [\n \"laravel\",\n \"framework\"\n ],", | ||
replace: ' "keywords": ["laravel", "framework"],' | ||
) | ||
->toString(); | ||
|
||
if (in_array(file_put_contents($composerPath, $formattedJson), [0, false], true)) { | ||
$this->components->error('Failed to write to composer.json.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$this->components->info('Added Markitdown setup script to composer.json'); | ||
} | ||
|
||
// Run the setup script | ||
$scriptPath = realpath(__DIR__.'/../../setup-python-env.sh'); | ||
|
||
if ($scriptPath === false || ! file_exists($scriptPath)) { | ||
$this->components->error('Setup script not found.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$this->components->info('Setting up Python virtual environment...'); | ||
|
||
// Make the script executable | ||
if (! chmod($scriptPath, 0755)) { | ||
$this->components->error('Failed to make setup script executable.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
// Run the setup script | ||
$pendingProcess = Process::path(dirname($scriptPath)) | ||
->tty(false) | ||
->timeout(300); | ||
|
||
$processResult = $pendingProcess->run($scriptPath); | ||
|
||
if (! $processResult->successful()) { | ||
$this->components->error('Failed to set up Python virtual environment: '.$processResult->errorOutput()); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$this->components->info('Markitdown installed successfully!'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
Oops, something went wrong.