Skip to content

Commit e629cf3

Browse files
committed
Bump dependencies and add flake.nix
1 parent 9983c6f commit e629cf3

File tree

8 files changed

+3698
-1694
lines changed

8 files changed

+3698
-1694
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
doc/
22
vendor/
3+
result

composer-env.nix

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
# This file originates from composer2nix
2+
3+
{ stdenv, lib, writeTextFile, fetchurl, php, unzip, phpPackages }:
4+
5+
let
6+
inherit (phpPackages) composer;
7+
buildZipPackage = { name, src }:
8+
stdenv.mkDerivation {
9+
inherit name src;
10+
buildInputs = [ unzip ];
11+
buildCommand = ''
12+
unzip $src
13+
baseDir=$(find . -type d -mindepth 1 -maxdepth 1)
14+
cd $baseDir
15+
mkdir -p $out
16+
mv * $out
17+
'';
18+
};
19+
20+
buildPackage =
21+
{ name
22+
, src
23+
, packages ? {}
24+
, devPackages ? {}
25+
, buildInputs ? []
26+
, symlinkDependencies ? false
27+
, executable ? false
28+
, removeComposerArtifacts ? false
29+
, postInstall ? ""
30+
, noDev ? false
31+
, composerExtraArgs ? ""
32+
, unpackPhase ? "true"
33+
, buildPhase ? "true"
34+
, ...}@args:
35+
36+
let
37+
reconstructInstalled = writeTextFile {
38+
name = "reconstructinstalled.php";
39+
executable = true;
40+
text = ''
41+
#! ${php}/bin/php
42+
<?php
43+
if(file_exists($argv[1]))
44+
{
45+
$composerLockStr = file_get_contents($argv[1]);
46+
47+
if($composerLockStr === false)
48+
{
49+
fwrite(STDERR, "Cannot open composer.lock contents\n");
50+
exit(1);
51+
}
52+
else
53+
{
54+
$config = json_decode($composerLockStr, true);
55+
56+
if(array_key_exists("packages", $config))
57+
$allPackages = $config["packages"];
58+
else
59+
$allPackages = array();
60+
61+
${lib.optionalString (!noDev) ''
62+
if(array_key_exists("packages-dev", $config))
63+
$allPackages = array_merge($allPackages, $config["packages-dev"]);
64+
''}
65+
66+
$packagesStr = json_encode($allPackages, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
67+
print($packagesStr);
68+
}
69+
}
70+
else
71+
print("[]");
72+
?>
73+
'';
74+
};
75+
76+
constructBin = writeTextFile {
77+
name = "constructbin.php";
78+
executable = true;
79+
text = ''
80+
#! ${php}/bin/php
81+
<?php
82+
$composerJSONStr = file_get_contents($argv[1]);
83+
84+
if($composerJSONStr === false)
85+
{
86+
fwrite(STDERR, "Cannot open composer.json contents\n");
87+
exit(1);
88+
}
89+
else
90+
{
91+
$config = json_decode($composerJSONStr, true);
92+
93+
if(array_key_exists("bin-dir", $config))
94+
$binDir = $config["bin-dir"];
95+
else
96+
$binDir = "bin";
97+
98+
if(array_key_exists("bin", $config))
99+
{
100+
if(!file_exists("vendor/".$binDir))
101+
mkdir("vendor/".$binDir);
102+
103+
foreach($config["bin"] as $bin)
104+
symlink("../../".$bin, "vendor/".$binDir."/".basename($bin));
105+
}
106+
}
107+
?>
108+
'';
109+
};
110+
111+
bundleDependencies = dependencies:
112+
lib.concatMapStrings (dependencyName:
113+
let
114+
dependency = dependencies.${dependencyName};
115+
in
116+
''
117+
${if dependency.targetDir == "" then ''
118+
vendorDir="$(dirname ${dependencyName})"
119+
mkdir -p "$vendorDir"
120+
${if symlinkDependencies then
121+
''ln -s "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
122+
else
123+
''cp -av "${dependency.src}" "$vendorDir/$(basename "${dependencyName}")"''
124+
}
125+
'' else ''
126+
namespaceDir="${dependencyName}/$(dirname "${dependency.targetDir}")"
127+
mkdir -p "$namespaceDir"
128+
${if symlinkDependencies then
129+
''ln -s "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
130+
else
131+
''cp -av "${dependency.src}" "$namespaceDir/$(basename "${dependency.targetDir}")"''
132+
}
133+
''}
134+
'') (builtins.attrNames dependencies);
135+
136+
extraArgs = removeAttrs args [ "name" "packages" "devPackages" "buildInputs" ];
137+
in
138+
stdenv.mkDerivation ({
139+
name = "composer-${name}";
140+
buildInputs = [ php composer ] ++ buildInputs;
141+
142+
inherit unpackPhase buildPhase;
143+
144+
installPhase = ''
145+
${if executable then ''
146+
mkdir -p $out/share/php
147+
cp -av $src $out/share/php/$name
148+
chmod -R u+w $out/share/php/$name
149+
cd $out/share/php/$name
150+
'' else ''
151+
cp -av $src $out
152+
chmod -R u+w $out
153+
cd $out
154+
''}
155+
156+
# Remove unwanted files
157+
rm -f *.nix
158+
159+
export HOME=$TMPDIR
160+
161+
# Remove the provided vendor folder if it exists
162+
rm -Rf vendor
163+
164+
# If there is no composer.lock file, compose a dummy file.
165+
# Otherwise, composer attempts to download the package.json file from
166+
# the registry which we do not want.
167+
if [ ! -f composer.lock ]
168+
then
169+
cat > composer.lock <<EOF
170+
{
171+
"packages": []
172+
}
173+
EOF
174+
fi
175+
176+
# Reconstruct the installed.json file from the lock file
177+
mkdir -p vendor/composer
178+
${php}/bin/php ${reconstructInstalled} composer.lock > vendor/composer/installed.json
179+
180+
# Copy or symlink the provided dependencies
181+
cd vendor
182+
${bundleDependencies packages}
183+
${lib.optionalString (!noDev) (bundleDependencies devPackages)}
184+
cd ..
185+
186+
# Reconstruct autoload scripts
187+
# We use the optimize feature because Nix packages cannot change after they have been built
188+
# Using the dynamic loader for a Nix package is useless since there is nothing to dynamically reload.
189+
composer dump-autoload --optimize ${lib.optionalString noDev "--no-dev"} ${composerExtraArgs}
190+
191+
# Run the install step as a validation to confirm that everything works out as expected
192+
composer install --optimize-autoloader ${lib.optionalString noDev "--no-dev"} ${composerExtraArgs}
193+
194+
${lib.optionalString executable ''
195+
# Reconstruct the bin/ folder if we deploy an executable project
196+
${php}/bin/php ${constructBin} composer.json
197+
ln -s $(pwd)/vendor/bin $out/bin
198+
''}
199+
200+
${lib.optionalString (!symlinkDependencies) ''
201+
# Patch the shebangs if possible
202+
if [ -d $(pwd)/vendor/bin ]
203+
then
204+
# Look for all executables in bin/
205+
for i in $(pwd)/vendor/bin/*
206+
do
207+
# Look for their location
208+
realFile=$(readlink -f "$i")
209+
210+
# Restore write permissions
211+
chmod u+wx "$(dirname "$realFile")"
212+
chmod u+w "$realFile"
213+
214+
# Patch shebang
215+
sed -e "s|#!/usr/bin/php|#!${php}/bin/php|" \
216+
-e "s|#!/usr/bin/env php|#!${php}/bin/php|" \
217+
"$realFile" > tmp
218+
mv tmp "$realFile"
219+
chmod u+x "$realFile"
220+
done
221+
fi
222+
''}
223+
224+
if [ "$removeComposerArtifacts" = "1" ]
225+
then
226+
# Remove composer stuff
227+
rm -f composer.json composer.lock
228+
fi
229+
230+
# Execute post install hook
231+
runHook postInstall
232+
'';
233+
} // extraArgs);
234+
in
235+
{
236+
composer = lib.makeOverridable composer;
237+
buildZipPackage = lib.makeOverridable buildZipPackage;
238+
buildPackage = lib.makeOverridable buildPackage;
239+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"svanderburg/pndp": "0.0.2"
1616
},
1717
"require-dev": {
18-
"phpdocumentor/phpdocumentor": "2.9.x"
18+
"phpdocumentor/phpdocumentor": "^3.0"
1919
},
2020

2121
"autoload": {

0 commit comments

Comments
 (0)