-
-
Save osiewicz/6afdd6626e517da24a2092807e6f0b6e to your computer and use it in GitHub Desktop.
Zed license filler
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
#!/bin/bash | |
# Define the path to the LICENSE file and the crates directory | |
LICENSE_FILE="LICENSE-AGPL" | |
LICENSE="Apache-2.0" | |
CRATES_DIR="./crates" | |
GPUI_CRATE_NAME="gpui" | |
all_gpui_deps=$(cargo tree -p gpui --prefix none) | |
if [[ -z "$all_gpui_deps" ]]; then | |
exit 1 | |
fi | |
workdir=$(pwd) | |
# Function to check if a crate is GPUI or one of its dependencies | |
is_gpui_or_dependency() { | |
local crate_name="$1" | |
local crate_path="$workdir/crates/$crate_name" | |
if ! [[ "$all_gpui_deps" == *"$crate_path"* ]]; then | |
echo "The crate '$crate_name' is NOT GPUI or a dependency." | |
else | |
echo "The crate '$crate_name' IS GPUI or a dependency." | |
fi | |
[[ "$all_gpui_deps" == *"$crate_path"* ]] | |
} | |
add_license_to_cargo() { | |
local crate_dir="$1" | |
local cargo_file="$crate_dir/Cargo.toml" | |
local tmp_file="$crate_dir/Cargo.toml.new" | |
# Use awk to insert the license field at the end of the [package] section | |
awk -v license="../../$LICENSE_FILE" ' | |
/^\[package\]/ { in_package = 1; print; next } | |
in_package && /^$/ { print "license-file = \"" license "\""; in_package = 0 } | |
{ print } | |
' "$cargo_file" > "$tmp_file" && mv "$tmp_file" "$cargo_file" | |
} | |
# Iterate over all the subdirectories in the crates directory | |
for crate_dir in "$CRATES_DIR"/*; do | |
# Check if the directory is a crate (by checking for Cargo.toml) | |
if [[ -f "$crate_dir/Cargo.toml" ]]; then | |
# Extract the crate name from the directory name | |
crate_name=$(basename "$crate_dir") | |
# Check if the crate is GPUI or one of its dependencies | |
if ! is_gpui_or_dependency "$crate_name"; then | |
echo "Updating crate: $crate_name" | |
# Add or update the license field in Cargo.toml to Apache2 | |
add_license_to_cargo "$crate_dir" | |
# Create a symlink to the LICENSE-APACHE2 file in the crate's directory | |
# It will be a relative symlink to the root LICENSE file | |
# (cd "$crate_dir" && ln -sfn ../../"$LICENSE_FILE" "$LICENSE_FILE") | |
fi | |
fi | |
done | |
echo "License update 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
#!/bin/bash | |
# Define the path to the LICENSE file and the crates directory | |
LICENSE_FILE="LICENSE-Apache2" | |
LICENSE="Apache2" | |
CRATES_DIR="./crates" | |
GPUI_CRATE_NAME="gpui" | |
all_gpui_deps=$(cargo tree -p gpui --prefix none) | |
if [[ -z "$all_gpui_deps" ]]; then | |
exit 1 | |
fi | |
workdir=$(pwd) | |
# Function to check if a crate is GPUI or one of its dependencies | |
is_gpui_or_dependency() { | |
local crate_name="$1" | |
local crate_path="$workdir/crates/$crate_name" | |
if ! [[ "$all_gpui_deps" == *"$crate_path"* ]]; then | |
echo "The crate '$crate_name' is NOT GPUI or a dependency." | |
else | |
echo "The crate '$crate_name' IS GPUI or a dependency." | |
fi | |
[[ "$all_gpui_deps" == *"$crate_path"* ]] | |
} | |
add_license_to_cargo() { | |
local crate_dir="$1" | |
local cargo_file="$crate_dir/Cargo.toml" | |
local tmp_file="$crate_dir/Cargo.toml.new" | |
# Use awk to insert the license field at the end of the [package] section | |
awk -v license="$LICENSE" ' | |
/^\[package\]/ { in_package = 1; print; next } | |
in_package && /^$/ { print "license = \"" license "\""; in_package = 0 } | |
{ print } | |
' "$cargo_file" > "$tmp_file" && mv "$tmp_file" "$cargo_file" | |
} | |
# Iterate over all the subdirectories in the crates directory | |
for crate_dir in "$CRATES_DIR"/*; do | |
# Check if the directory is a crate (by checking for Cargo.toml) | |
if [[ -f "$crate_dir/Cargo.toml" ]]; then | |
# Extract the crate name from the directory name | |
crate_name=$(basename "$crate_dir") | |
# Check if the crate is GPUI or one of its dependencies | |
if is_gpui_or_dependency "$crate_name"; then | |
echo "Updating crate: $crate_name" | |
# Add or update the license field in Cargo.toml to Apache2 | |
add_license_to_cargo "$crate_dir" | |
# Create a symlink to the LICENSE-APACHE2 file in the crate's directory | |
# It will be a relative symlink to the root LICENSE file | |
(cd "$crate_dir" && ln -sfn ../../"$LICENSE_FILE" "$LICENSE_FILE") | |
fi | |
fi | |
done | |
echo "License update complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment