Skip to content

Commit

Permalink
casks: Fix cask generation for 'deep' archives [skip ci]
Browse files Browse the repository at this point in the history
[why]
Normally the release zip files have a flat directory structure, i.e. all
files are in the base directory and there are no subdirs. The existing
subdirectory structure is removed on zip creation via `archive-fonts.sh`.

If the patch results have two fonts with identical name these flat
archives can not be generated. The `archive-fonts.sh` reports:

    Could not create archive with the path junked (-j option) - likely
    same font names for different paths, zip status: $zipStatus
    Retrying with full path

But now the problem is only delayed. When the font file has the name
name the Family and SubFamily are most likely also the same for both
font files. If we install both the user (and the system) can not really
distinguish the font files anymore.

This turned up with [1] when doing a `brew style *nerd-font*` that
complains in fact about duplicate fonts to install. At that point we had
the additional problem that the subdir-path had been missing in the cask
files. So I'm not sure if it would work from a Cask standpoint (i.e.
font files are installed in subdirs).

Anyhow, this will fail when the user wants to select the font files,
because normally this is done on a Family-name basis. And identical file
name usually means identical Family-SubFamily.

Note that this ONLY happens with GohuFont, which has these subdirs:

    11/complete
    14/complete
    uni-11/complete
    uni-14/complete

The font has embedded bitmaps, but only one size. The "11" ones have
11px bitmap fonts and the "14" ones 14px bitmaps.
The "uni-" variant has much more glyphs.
Otherwise the differences are slim. From the font creation date the
"uni-" ones are newer.

[how]
Each font filename added to a cask is memorized. When the next fontfile
is to be added to the cask it is checked if we already have that
filename installed. Duplicates are then skipped.

If we have a deep archive, the fonts are added to the cask with full
relative path.

In case of Gohu that means the 11/complete variant is added (because all
files are alphabetically sorted before adding). By chance this is the
same variant that has historically been in the Cask, so no change here.

Fixes:

ryanoasis#1008 (comment)
Homebrew/homebrew-cask-fonts#6758 (comment)

Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
  • Loading branch information
Finii committed Dec 14, 2022
1 parent e485fc9 commit 7926b9b
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions bin/scripts/generate-casks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ function find_common_stem {
done
}

# Check if an element already exists in an array
function contains {
local what=$1
shift
for e; do
if [ "${e}" = "${what}" ]; then
return 0
fi
done
return 1
}

function write_body {
local unpatchedname=$1
local outputfile=$2
Expand Down Expand Up @@ -131,11 +143,22 @@ function write_body {
fi
familyname="$(tr [:lower:] [:upper:] <<< ${familyname:0:1})${familyname:1}"
# Process font files
local all_individual_fonts=( )
local warned=0
for i in "${!fonts[@]}"; do
local individualfont=$(basename "${fonts[$i]}")
local individualdir=$(dirname "${fonts[$i]}")

printf " %-${longest}s %s\\n" "${individualfont}" "${individualdir}/"
if [ $(dirname "${individualdir}") = "." ]; then
individualdir=""
else
if [ ${warned} -eq 0 ]; then
echo "$LINE_PREFIX WARNING: Non-flat directory structure in archive! We might have problems..."
warned=1
fi
# Remove leftmost directory (on Linux), the base directory of the zip file
individualdir=$(sed -E 's!/+!/!;s!^([^/]*/)|(/[^/]+/?)|([^/]*$)!!' <<< "${individualdir}")/
fi

if [ "$i" == 0 ]; then
{
Expand All @@ -150,7 +173,19 @@ function write_body {
} >> "$outputfile"
fi

printf " font \"%s\"\\n" "$individualfont" >> "$outputfile"
# When we have a 'deep' directory structure there can be multiple files with the same name
# (in different subdirectories). We can not install two files with the same name, that does
# not make sense. Someone has to choose which alternative is wanted.
# Here we just take the first one.
# This is only occuring at the moment with GohuFont.
$(contains "$individualfont" "${all_individual_fonts[@]}") && {
printf " SKIPPING: %-${longest}s %s\\n" "${individualfont}" "${individualdir}/"
continue
}
all_individual_fonts+=("$individualfont")

printf " font \"%s\"\\n" "${individualdir}${individualfont}" >> "${outputfile}"
printf " %-${longest}s %s\\n" "${individualfont}" "${individualdir}/"

done
else
Expand Down

0 comments on commit 7926b9b

Please sign in to comment.