(1) Rename files to snake_case: scripts
do-renames.sh: Take a list of files to rename and do perform the renaming (includes updating BUILD.gn, include guards, DEPS, include paths, and installing forwarding headers). find_header_renames.sh: Looks through all header files and tries to guess what they should be renamed to, if they don't already have underscores. find_source_test_renames.sh: Takes a list of header file renames and applies that information to renaming the corresponding source/test files. Bug: webrtc:10159 No-Presubmit: true No-Tree-Checks: true No-Try: true Change-Id: I073608e20bb163f3923ab2209eea72a115a4f593 Reviewed-on: https://webrtc-review.googlesource.com/c/91900 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#26223}
This commit is contained in:
233
tools_webrtc/do-renames.sh
Executable file
233
tools_webrtc/do-renames.sh
Executable file
@ -0,0 +1,233 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||
#
|
||||
# Use of this source code is governed by a BSD-style license
|
||||
# that can be found in the LICENSE file in the root of the source
|
||||
# tree. An additional intellectual property rights grant can be found
|
||||
# in the file PATENTS. All contributing project authors may
|
||||
# be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
# Run various operations according to the rename operations specified in the
|
||||
# given rename file.
|
||||
#
|
||||
# The rename file must have the following format:
|
||||
#
|
||||
# <old path 1> --> <new path 1>
|
||||
# <old path 2> --> <new path 2>
|
||||
#
|
||||
# For example:
|
||||
#
|
||||
# a/old_name.h --> a/new_name.h
|
||||
# # Comments are allowed.
|
||||
# b/old.h --> b/new.h
|
||||
#
|
||||
|
||||
if [[ $# -lt 2 ]]; then
|
||||
echo "Usage: $0 <move|update|install> <path to rename file>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cmd="$1"
|
||||
rename_file="$2"
|
||||
|
||||
# replace_in_file $fn $old $new $path
|
||||
#
|
||||
# Replaces in the file at $path the result of applying $fn to $old with the
|
||||
# result of applying $fn to $new.
|
||||
function replace_in_file {
|
||||
fn="$1"
|
||||
old="$2"
|
||||
new="$3"
|
||||
path="$4"
|
||||
sed -i "s!$($fn "$old")!$($fn "$new")!g" "$path"
|
||||
}
|
||||
|
||||
# Moves the file using git.
|
||||
function move_file {
|
||||
old_path="$1"
|
||||
new_path="$2"
|
||||
git mv "$old_path" "$new_path"
|
||||
echo "Moved $old_path to $new_path"
|
||||
}
|
||||
|
||||
# Outputs the path to the relevant BUILD.gn file for the given file path.
|
||||
# Returns 0 if found, 1 otherwise.
|
||||
function find_build_file {
|
||||
path="$1"
|
||||
file_name="$(basename "$path")"
|
||||
dir_name="$(dirname "$path")"
|
||||
while [ "$dir_name" != "." ]; do
|
||||
build_path="${dir_name}/BUILD.gn"
|
||||
if [ -f "$build_path" ] && grep "\"$file_name\"" "$build_path" \
|
||||
> /dev/null; then
|
||||
echo "$build_path"
|
||||
return 0
|
||||
fi
|
||||
file_name="$(basename "$dir_name")/$file_name"
|
||||
dir_name="$(dirname "$dir_name")"
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Update the relevant BUILD.gn file with the renamed file.
|
||||
function rename_in_build_file {
|
||||
old_path="$1"
|
||||
new_path="$2"
|
||||
build_path=$(find_build_file "$old_path")
|
||||
if [[ $? -ne 0 ]]; then
|
||||
return 1
|
||||
fi
|
||||
build_dir="$(dirname "$build_path")/"
|
||||
old_name=$(echo "$old_path" | sed "s#$build_dir##")
|
||||
new_name=$(echo "$new_path" | sed "s#$build_dir##")
|
||||
sed -i "s#\"$old_name\"#\"$new_name\"#g" "$build_path"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Update the relevant DEPS files with the renamed file.
|
||||
function rename_in_deps {
|
||||
old_path="$1"
|
||||
new_path="$2"
|
||||
|
||||
# First, update other DEPS referencing this file.
|
||||
function deps_reference {
|
||||
echo "\"+$1\""
|
||||
}
|
||||
count=0
|
||||
while read -r referencer_path && [[ -n "$referencer_path" ]]; do
|
||||
replace_in_file deps_reference "$old_path" "$new_path" "$referencer_path"
|
||||
let count=count+1
|
||||
done <<< "$(git grep --files-with-matches $(deps_reference "$old_path") \
|
||||
'*/DEPS')"
|
||||
echo -n $count
|
||||
|
||||
# Second, update DEPS specifying this file.
|
||||
function deps_entry {
|
||||
echo "\"$(basename "$1" .h)\\\\\\.h\":"
|
||||
}
|
||||
dir_name=$(dirname "$old_path")
|
||||
while [ "$dir_name" != "." ]; do
|
||||
deps_path="${dir_name}/DEPS"
|
||||
if [ -f "$deps_path" ]; then
|
||||
replace_in_file deps_entry "$old_path" "$new_path" "$deps_path"
|
||||
break
|
||||
fi
|
||||
dir_name=$(dirname "$dir_name")
|
||||
done
|
||||
}
|
||||
|
||||
# Update all #include references from the old header path to the new path.
|
||||
function update_all_includes {
|
||||
old_header_path="$1"
|
||||
new_header_path="$2"
|
||||
count=0
|
||||
while read -r includer_path && [[ -n "$includer_path" ]]; do
|
||||
sed -i "s!#include \"$old_header_path\"!#include \"$new_header_path\"!g" \
|
||||
"$includer_path"
|
||||
let count=count+1
|
||||
done <<< "$(git grep --files-with-matches "#include \"$old_header_path\"")"
|
||||
echo -n $count
|
||||
}
|
||||
|
||||
# Echo out the header guard for a given file path.
|
||||
# E.g., api/jsep.h turns into API_JSEP_H_ .
|
||||
function header_guard {
|
||||
echo "${1}_" | perl -pe 's/[\/\.-]/_/g' | perl -pe 's/(.)/\U$1/g'
|
||||
}
|
||||
|
||||
# Updates BUILD.gn and (if header) the include guard and all #include
|
||||
# references.
|
||||
function update_file {
|
||||
old_path="$1"
|
||||
new_path="$2"
|
||||
echo -n "Processing $old_path --> $new_path ... "
|
||||
echo -n " build file ... "
|
||||
if rename_in_build_file "$old_path" "$new_path"; then
|
||||
echo -n done
|
||||
else
|
||||
echo -n failed
|
||||
fi
|
||||
if [[ "$old_path" == *.h ]]; then
|
||||
echo -n " header guard ... "
|
||||
old_header_guard=$(header_guard "$old_path")
|
||||
new_header_guard=$(header_guard "$new_path")
|
||||
sed -i "s/${old_header_guard}/${new_header_guard}/g" "$new_path"
|
||||
echo -n done
|
||||
echo -n " includes ... "
|
||||
update_all_includes "$old_path" "$new_path"
|
||||
echo -n " done"
|
||||
echo -n " deps ... "
|
||||
rename_in_deps "$old_path" "$new_path"
|
||||
echo -n " done"
|
||||
fi
|
||||
echo
|
||||
}
|
||||
|
||||
# Generate forwarding headers for the old header path that include the new
|
||||
# header path.
|
||||
function install_file {
|
||||
old_path="$1"
|
||||
new_path="$2"
|
||||
if ! [[ "$old_path" == *.h ]]; then
|
||||
return
|
||||
fi
|
||||
if ! [ -f "$old_path" ]; then
|
||||
# Add the old path to the BUILD.gn file.
|
||||
build_path="$(find_build_file "$new_path")"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
build_dir="$(dirname "$build_path")/"
|
||||
old_name=$(echo "$old_path" | sed "s#$build_dir##")
|
||||
new_name=$(echo "$new_path" | sed "s#$build_dir##")
|
||||
sed -i "s!^\\([^#]*\\)\"$new_name\"!\1\"$new_name\",\"$old_name\"!g" \
|
||||
"$build_path"
|
||||
fi
|
||||
fi
|
||||
old_header_guard=$(header_guard "$old_path")
|
||||
cat << EOF > "$old_path"
|
||||
/*
|
||||
* Copyright $(date +%Y) The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef $old_header_guard
|
||||
#define $old_header_guard
|
||||
|
||||
// TODO(bugs.webrtc.org/10159): Remove this files once downstream projects have
|
||||
// been updated to include the new path.
|
||||
|
||||
#include "$new_path"
|
||||
|
||||
#endif // $old_header_guard
|
||||
EOF
|
||||
git add "$old_path"
|
||||
echo "Installed header at $old_path pointing to $new_path"
|
||||
}
|
||||
|
||||
IFS=$'\n'
|
||||
for rename_stanza in $(cat "$rename_file" | grep -v '^#'); do
|
||||
IFS=$' '
|
||||
arr=($rename_stanza)
|
||||
old_path=${arr[0]}
|
||||
new_path=${arr[2]}
|
||||
case "$cmd" in
|
||||
"move")
|
||||
move_file "$old_path" "$new_path"
|
||||
;;
|
||||
"update")
|
||||
update_file "$old_path" "$new_path"
|
||||
;;
|
||||
"install")
|
||||
install_file "$old_path" "$new_path"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown command: $cmd"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
71
tools_webrtc/find_header_renames.sh
Executable file
71
tools_webrtc/find_header_renames.sh
Executable file
@ -0,0 +1,71 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||
#
|
||||
# Use of this source code is governed by a BSD-style license
|
||||
# that can be found in the LICENSE file in the root of the source
|
||||
# tree. An additional intellectual property rights grant can be found
|
||||
# in the file PATENTS. All contributing project authors may
|
||||
# be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
# Usage: find_header_renames.sh {dir}
|
||||
#
|
||||
# Looks for headers in the git repository and suggests renames to add
|
||||
# underscores to files that are missing them.
|
||||
#
|
||||
# Outputs a sorted sequence of renames in the form:
|
||||
# d/hdr1.h --> d/hdr2.h
|
||||
#
|
||||
# If the rename could not be automatically deduced, the output will look like:
|
||||
# d/hdr1.h --> d/???.h
|
||||
#
|
||||
|
||||
for old_path in $(git ls-files "${1:+$1/}*.h"); do
|
||||
# Extract the file name (without the .h).
|
||||
old_name=$(basename "$old_path" .h)
|
||||
|
||||
# If there is an underscore already, assume it does not need to be renamed.
|
||||
if [[ "$old_name" =~ _ ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# If there are capital letters it's likely an Objective C file which does
|
||||
# not need to be renamed.
|
||||
if [[ "$old_name" =~ [A-Z] ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# We need to know where to put the underscores, so try a heuristic:
|
||||
# 1. Look for any sequence in the file that matches (case insensitively) the
|
||||
# file name.
|
||||
# 2. Remove any results which are either all lower case or all upper case
|
||||
# (these aren't going to help).
|
||||
# 3. Convert the results (in camel case) into snake case.
|
||||
# 4. Deduplicate.
|
||||
#
|
||||
# If there is only one result then we're good: there's an unambiguous
|
||||
# translation in the file into snake case. Otherwise, we throw up our hands
|
||||
# and defer to a human.
|
||||
candidates=$(
|
||||
cat "$old_path" |
|
||||
sed 's/^.*PROXY_MAP(\(.*\))$/class \1Proxy/' |
|
||||
grep -io "$old_name" |
|
||||
grep -v "$old_name" |
|
||||
grep -v $(echo "$old_name" | tr '[:lower:]' '[:upper:]') |
|
||||
perl -pe 's/([A-Z][a-z])/_$1/g' |
|
||||
perl -pe 's/^_?//' |
|
||||
tr '[:upper:]' '[:lower:]' |
|
||||
sort -u)
|
||||
|
||||
if [[ $(echo "$candidates" | wc -w) -eq 1 ]]; then
|
||||
# We only have one candidate: great! This is most likely correct.
|
||||
# If the candidate is the same as the file name, then no need to rename.
|
||||
if [ "$old_name" == "$candidates" ]; then
|
||||
continue
|
||||
fi
|
||||
echo "$old_path --> $(dirname "$old_path")/${candidates}.h"
|
||||
else
|
||||
# Either got 0 candidates or more than 1, need human intervention.
|
||||
echo "$old_path --> $(dirname "$old_path")/???.h"
|
||||
fi
|
||||
done | sort
|
55
tools_webrtc/find_source_test_renames.sh
Executable file
55
tools_webrtc/find_source_test_renames.sh
Executable file
@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||
#
|
||||
# Use of this source code is governed by a BSD-style license
|
||||
# that can be found in the LICENSE file in the root of the source
|
||||
# tree. An additional intellectual property rights grant can be found
|
||||
# in the file PATENTS. All contributing project authors may
|
||||
# be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
# Usage: find_source_test_renames.sh {header-renames}
|
||||
#
|
||||
# Given a file with header renames in the form:
|
||||
# d/hdr1.h --> d/hdr2.h
|
||||
# Outputs a sorted sequence of renames that also include .cc and _unittest.cc
|
||||
# renames that match the header renames.
|
||||
#
|
||||
|
||||
line_regex="([^ ]+) --> ([^ ]+)"
|
||||
while ((line_no++)); read line; do
|
||||
echo "$line"
|
||||
|
||||
if ! [[ $line =~ $line_regex ]]; then
|
||||
(>&2 echo "$line_no: Skipping malformed line: $line")
|
||||
continue
|
||||
fi
|
||||
|
||||
old_path="${BASH_REMATCH[1]}"
|
||||
new_path="${BASH_REMATCH[2]}"
|
||||
if ! [[ -f "$old_path" ]]; then
|
||||
(>&2 echo "$line_no: Skipping missing old path: $old_path")
|
||||
continue
|
||||
fi
|
||||
|
||||
old_name="$(basename "$old_path" .h)"
|
||||
new_name="$(basename "$new_path" .h)"
|
||||
|
||||
if [[ "$new_name" == "???" ]]; then
|
||||
(>&2 echo "$line_no: Skipping missing new name: $new_name")
|
||||
continue
|
||||
fi
|
||||
|
||||
|
||||
# Find source renames.
|
||||
for old_source_path in $(git ls-files "*/${old_name}.cc"); do
|
||||
new_source_path="$(dirname "$old_source_path")/${new_name}.cc"
|
||||
echo "$old_source_path --> $new_source_path"
|
||||
done
|
||||
|
||||
# Find unittest renames.
|
||||
for old_test_path in $(git ls-files "*/${old_name}_unittest.cc"); do
|
||||
new_test_path="$(dirname "$old_test_path")/${new_name}_unittest.cc"
|
||||
echo "$old_test_path --> $new_test_path"
|
||||
done
|
||||
done < "${1:-/dev/stdin}" | sort -u
|
Reference in New Issue
Block a user