<commit_before>#!/bin/bash
#* This file is part of the MOOSE framework
#* https://www.mooseframework.org
#*
#* All rights reserved, see COPYRIGHT for full restrictions
#* https://github.com/idaholab/moose/blob/master/COPYRIGHT
#*
#* Licensed under LGPL 2.1, please see LICENSE for details
#* https://www.gnu.org/licenses/lgpl-2.1.html

function printusage {
    echo "Usage:    stork.sh <name>"
    echo ""
    echo "    Creates a new blank MOOSE app in the current working directory."
    echo "    <name> should be given in CamelCase format.  Allowed are letters and numbers "
    echo "           (cannot start with a number). No special characters are allowed."
    echo "    When --module is supplied after the <name> a MOOSE module will be created."
}

if [[ "$1" == "-h" || "$1" == "--help" || $# == 0 || $# > 2 ]]; then
    printusage
    exit 1
fi

if [[ $# == 2 && "$2" != "--module" ]]; then
    printusage
    exit 1
fi


if [[ $# == 2 && "$2" == "--module" ]]; then
    kind="module"
else
    kind="app"
fi

# set old/new app name variables
srcname='Stork'
dstname=$1

# check that dstname does not contain a special character
if [[ $dstname == *[\!\@\#\$\%\^\&\*\(\)\-\+]* ]] ; then
  echo "error: provided name contains a special character."
  exit 1
fi
# check that the name does not start with a number
if [[ ${dstname:0:1} == *[0-9]* ]] ; then
  echo "error: the first character of the name is a number."
  exit 1
fi

regex='s/([A-Z][a-z])/_\1/g; s/([a-z])([A-Z])/\1_\2/g; s/^_//;'

srcnamelow=$(echo "$srcname" | perl -pe "${regex}"'tr/[A-Z]/[a-z]/')
srcnameup=$(echo "$srcname" | perl -pe "${regex}"'tr/[a-z]/[A-Z]/')
dstnamelow=$(echo "$dstname" | perl -pe "${regex}"'tr/[A-Z]/[a-z]/')
dstnameup=$(echo "$dstname" | perl -pe "${regex}"'tr/[a-z]/[A-Z]/')
dir="$dstnamelow"
if [[ -z $MOOSE_DIR ]]; then
    MOOSE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."
fi
if [[ "$kind" == "module" ]]; then
    dir="${MOOSE_DIR}/modules/$dir"
    if [[ ! -w "${MOOSE_DIR}/modules" ]] ; then
        echo "error: cannot create module without write permissions to $dir" ;
        exit 1
    fi
fi
if [[ ! -f "$MOOSE_DIR/stork/include/base/StorkApp.h" ]]; then
    echo "error: cannot find usable MOOSE directory"
    exit 1
fi

if [[ "$kind" == "app" ]]; then
    git status &>/dev/null && echo "error: your current working directory is inside a git repository" >&2 && exit 1
fi

# copy stork tree - abort if dir already exists or copy fails
if [[ -d "$dir" ]]; then
    echo "error: directory '$dir' already exists" >&2
    exit 1
fi
cp -R "$MOOSE_DIR/stork" "$dir" || echo "error: app/module creation failed" >&2 || exit 1

find $dir -type f -name '.*' | xargs rm -f # remove hidden files (e.g. vim swp files)

# rename app name within files
function recursiveRename {
    src=$1
    dst=$2
    grep --recursive -l "$src" $dir | xargs sed -i.bak 's/'"$src"'/'"$dst"'/g'
    find $dir -type f -name '*.bak' | xargs rm -f
}
recursiveRename "$srcname" "$dstname"
recursiveRename "$srcnamelow" "$dstnamelow"
recursiveRename "$srcnameup" "$dstnameup"

# rename files
mv "$dir/Makefile.${kind}" "$dir/Makefile"
mv "$dir/unit/Makefile.${kind}" "$dir/unit/Makefile"
mv "$dir/run_tests.${kind}" "$dir/run_tests"
mv "$dir/src/base/${srcname}App.C.${kind}" "$dir/src/base/${dstname}App.C"
mv "$dir/test/src/base/${srcname}TestApp.C.${kind}" "$dir/test/src/base/${dstname}TestApp.C"
mv "$dir/include/base/${srcname}App.h" "$dir/include/base/${dstname}App.h"
mv "$dir/test/include/base/${srcname}TestApp.h" "$dir/test/include/base/${dstname}TestApp.h"
mv "$dir/doc/config.yml.${kind}" "$dir/doc/config.yml"
mv "$dir/doc/moosedocs.py.${kind}" "$dir/doc/moosedocs.py"
chmod a+x "$dir/doc/moosedocs.py"
chmod a+x "$dir/run_tests"

# remove unnecessary files
rm -f $dir/Makefile.*
rm -f $dir/unit/Makefile.*
rm -f $dir/run_tests.*
rm -f $dir/src/base/StorkApp.C.*
rm -f $dir/test/src/base/StorkTestApp.C.*
rm -f $dir/doc/config.yml.*
rm -f $dir/doc/moosedocs.py.*

if [[ "$kind" == "app" ]]; then
    # copy clang-format related files
    mkdir -p $dir/scripts
    cp $MOOSE_DIR/.clang-format $dir/
    cp $MOOSE_DIR/.gitignore $dir/

    dir="$PWD/$dir"
    (cd $dir && git init && git branch -m main && git add * .clang-format .gitignore && git commit -m"Initial files")

    echo "MOOSE app created in '$dir'"
    echo ""
    echo "To store your changes on github:"<commit_msg>Make stork script a bit more backwards compatible with branch creation and renaming (#22110)<commit_after>#!/bin/bash
#* This file is part of the MOOSE framework
#* https://www.mooseframework.org
#*
#* All rights reserved, see COPYRIGHT for full restrictions
#* https://github.com/idaholab/moose/blob/master/COPYRIGHT
#*
#* Licensed under LGPL 2.1, please see LICENSE for details
#* https://www.gnu.org/licenses/lgpl-2.1.html

function printusage {
    echo "Usage:    stork.sh <name>"
    echo ""
    echo "    Creates a new blank MOOSE app in the current working directory."
    echo "    <name> should be given in CamelCase format.  Allowed are letters and numbers "
    echo "           (cannot start with a number). No special characters are allowed."
    echo "    When --module is supplied after the <name> a MOOSE module will be created."
}

if [[ "$1" == "-h" || "$1" == "--help" || $# == 0 || $# > 2 ]]; then
    printusage
    exit 1
fi

if [[ $# == 2 && "$2" != "--module" ]]; then
    printusage
    exit 1
fi


if [[ $# == 2 && "$2" == "--module" ]]; then
    kind="module"
else
    kind="app"
fi

# set old/new app name variables
srcname='Stork'
dstname=$1

# check that dstname does not contain a special character
if [[ $dstname == *[\!\@\#\$\%\^\&\*\(\)\-\+]* ]] ; then
  echo "error: provided name contains a special character."
  exit 1
fi
# check that the name does not start with a number
if [[ ${dstname:0:1} == *[0-9]* ]] ; then
  echo "error: the first character of the name is a number."
  exit 1
fi

regex='s/([A-Z][a-z])/_\1/g; s/([a-z])([A-Z])/\1_\2/g; s/^_//;'

srcnamelow=$(echo "$srcname" | perl -pe "${regex}"'tr/[A-Z]/[a-z]/')
srcnameup=$(echo "$srcname" | perl -pe "${regex}"'tr/[a-z]/[A-Z]/')
dstnamelow=$(echo "$dstname" | perl -pe "${regex}"'tr/[A-Z]/[a-z]/')
dstnameup=$(echo "$dstname" | perl -pe "${regex}"'tr/[a-z]/[A-Z]/')
dir="$dstnamelow"
if [[ -z $MOOSE_DIR ]]; then
    MOOSE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."
fi
if [[ "$kind" == "module" ]]; then
    dir="${MOOSE_DIR}/modules/$dir"
    if [[ ! -w "${MOOSE_DIR}/modules" ]] ; then
        echo "error: cannot create module without write permissions to $dir" ;
        exit 1
    fi
fi
if [[ ! -f "$MOOSE_DIR/stork/include/base/StorkApp.h" ]]; then
    echo "error: cannot find usable MOOSE directory"
    exit 1
fi

if [[ "$kind" == "app" ]]; then
    git status &>/dev/null && echo "error: your current working directory is inside a git repository" >&2 && exit 1
fi

# copy stork tree - abort if dir already exists or copy fails
if [[ -d "$dir" ]]; then
    echo "error: directory '$dir' already exists" >&2
    exit 1
fi
cp -R "$MOOSE_DIR/stork" "$dir" || echo "error: app/module creation failed" >&2 || exit 1

find $dir -type f -name '.*' | xargs rm -f # remove hidden files (e.g. vim swp files)

# rename app name within files
function recursiveRename {
    src=$1
    dst=$2
    grep --recursive -l "$src" $dir | xargs sed -i.bak 's/'"$src"'/'"$dst"'/g'
    find $dir -type f -name '*.bak' | xargs rm -f
}
recursiveRename "$srcname" "$dstname"
recursiveRename "$srcnamelow" "$dstnamelow"
recursiveRename "$srcnameup" "$dstnameup"

# rename files
mv "$dir/Makefile.${kind}" "$dir/Makefile"
mv "$dir/unit/Makefile.${kind}" "$dir/unit/Makefile"
mv "$dir/run_tests.${kind}" "$dir/run_tests"
mv "$dir/src/base/${srcname}App.C.${kind}" "$dir/src/base/${dstname}App.C"
mv "$dir/test/src/base/${srcname}TestApp.C.${kind}" "$dir/test/src/base/${dstname}TestApp.C"
mv "$dir/include/base/${srcname}App.h" "$dir/include/base/${dstname}App.h"
mv "$dir/test/include/base/${srcname}TestApp.h" "$dir/test/include/base/${dstname}TestApp.h"
mv "$dir/doc/config.yml.${kind}" "$dir/doc/config.yml"
mv "$dir/doc/moosedocs.py.${kind}" "$dir/doc/moosedocs.py"
chmod a+x "$dir/doc/moosedocs.py"
chmod a+x "$dir/run_tests"

# remove unnecessary files
rm -f $dir/Makefile.*
rm -f $dir/unit/Makefile.*
rm -f $dir/run_tests.*
rm -f $dir/src/base/StorkApp.C.*
rm -f $dir/test/src/base/StorkTestApp.C.*
rm -f $dir/doc/config.yml.*
rm -f $dir/doc/moosedocs.py.*

if [[ "$kind" == "app" ]]; then
    # copy clang-format related files
    mkdir -p $dir/scripts
    cp $MOOSE_DIR/.clang-format $dir/
    cp $MOOSE_DIR/.gitignore $dir/

    dir="$PWD/$dir"
    (cd $dir && git init && git add * .clang-format .gitignore && git commit -m "Initial files" && git branch -m main)

    echo "MOOSE app created in '$dir'"
    echo ""
    echo "To store your changes on github:"
{'fraction_non_alphanumeric': 0.19943502824858758, 'fraction_numerical': 0.00847457627118644, 'mean_word_length': 3.323888617489008, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4178994', 'n_tokens_mistral': 3392, 'n_tokens_neox': 3306, 'n_words': 943}