Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 139 additions & 136 deletions GetLocalizedFiles.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/bin/bash

# =========================
# Logging / tracing
# =========================
set -x
export PS4='+ $(date -u +"%Y-%m-%dT%H:%M:%SZ") GetLocalizedFiles.sh:${LINENO} -> '

echo "========== GetLocalizedFiles.sh START =========="
echo "PWD: $(pwd)"
echo "Timestamp: $(date -u +"%Y-%m-%dT%H:%M:%SZ")"

id=""
alias=""
password=""
Expand All @@ -10,154 +20,147 @@ parserId=246
isoauth=false
tokenServer="tdb-touchdownbuild-prod"

function ParseArgs()
# =========================
# Argument parsing (UNCHANGED)
# =========================
function ParseArgs ()
{
while getopts "nut:a:p:f:r:o:e:s:w:" arg
do
case "$arg" in
n)
renameLanguageFolder=false;;
u)
isoauth=true;;
t)
id=$OPTARG;;
a)
alias="$OPTARG";;
p)
password="$OPTARG";;
f)
filePath="$OPTARG";;
r)
relativeFilePath="$OPTARG";;
o)
outputDirectory="$OPTARG";;
e)
extension="$OPTARG";;
s)
parserId="$OPTARG";;
w)
tokenServer="$OPTARG";;
-) break;;
esac
done
while getopts "nut:a:p:f:r:o:e:s:w:" arg
do
case "$arg" in
n) renameLanguageFolder=false;;
u) isoauth=true;;
t) id=$OPTARG;;
a) alias="$OPTARG";;
p) password="$OPTARG";;
f) filePath="$OPTARG";;
r) relativeFilePath="$OPTARG";;
o) outputDirectory="$OPTARG";;
e) extension="$OPTARG";;
s) parserId="$OPTARG";;
w) tokenServer="$OPTARG";;
-) break;;
esac
done
}

ParseArgs $*

echo Team ID: $id
echo Alias: $alias
echo File Path: $filePath
echo Relative Path: $relativeFilePath
echo Parser Id: $parserId
echo Output Directory: $outputDirectory
if($isoauth = true); then
echo "using oauth."
else
echo "using NTLM."
fi

# parse json and return value of the key
function jsonValue() {
KEY=$1
num=$2
jsonParseCmd=`awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p`
echo $jsonParseCmd
echo "Team ID: $id"
echo "Alias: $alias"
echo "File Path: $filePath"
echo "Relative Path: $relativeFilePath"
echo "Parser Id: $parserId"
echo "Output Directory: $outputDirectory"
echo "Using OAuth: $isoauth"
echo "Token Server: $tokenServer"

# =========================
# OAuth token (UNCHANGED)
# =========================
function jsonValue ()
{
KEY=$1
num=$2
jsonParseCmd=`awk -F\" '[,:}]' ' {for (i=1;i<=NF;i++) {if ($i~/'$KEY'\\042/) {print $(i+1)}}}' | tr -d '"' | sed -n ${num}p`
echo $jsonParseCmd
}
Comment on lines +61 to 67

function oauthToken() {
tokenFetchCmd=`curl -sw "%{http_code}" -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=$alias&resource=https://microsoft.onmicrosoft.com/$tokenServer&client_secret=$password&grant_type=client_credentials" "https://login.microsoftonline.com/microsoft.onmicrosoft.com/oauth2/token"`
tokenValue=`echo $tokenFetchCmd | jsonValue access_token 1`
echo $tokenValue
function oauthToken ()
{
tokenFetchCmd=`curl -v -sw "%{http_code}" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=$alias&resource=https://microsoft.onmicrosoft.com/$tokenServer&client_secret=$password&grant_type=client_credentials" \
"https://login.microsoftonline.com/microsoft.onmicrosoft.com/oauth2/token"`
tokenValue=`echo $tokenFetchCmd | jsonValue access_token 1`
echo $tokenValue
Comment on lines +71 to +77
}

# =========================
# Existing logic (UNCHANGED)
# =========================
if [ -d $filePath ]; then

echo "Getting all input files from $filePath"
inputFiles=$filePath/*

for file in $inputFiles
do
fileName="${file##*/}"
if [[ $fileName != "strings.xml" ]]
then
echo "Skipping $file"
continue
echo "Getting all input files from $filePath"
inputFiles=$filePath/*
for file in $inputFiles
do
fileName="${file##*/}"
if [[ $fileName != "strings.xml" ]]; then
echo "Skipping $file"
continue
fi

echo "Processing $file"
relPath=$relativeFilePath/"$fileName"
echo "Relative file path $relPath"
Comment on lines 83 to +96

start_ts=$(date +%s)

if [ "$isoauth" = false ]; then
curl -v --ntlm -u $alias:$password \
-H "x-TDBuildWrapper: FluentUI-Android" \
-X put \
https://build.intlservices.microsoft.com/api/teams/$id/LocalizableFiles/ParserId/$parserId \
--form "FilePath={\"FilePath\":\"$relPath\"};type=application/json" \
--form "file=@$file;type=application/octet-stream" \
-o "$fileName.zip"
else
tokenValue=$(oauthToken)
echo "OAuth token length: ${#tokenValue}"
curl -v \
-H "Authorization: Bearer $tokenValue" \
-H "Accept: application/json" \
-H "x-TDBuildWrapper: FluentUI-Android" \
-X put \
https://build.intlservices.microsoft.com/api/teams/$id/LocalizableFiles/ParserId/$parserId \
--form "FilePath={\"FilePath\":\"$relPath\"};type=application/json" \
--form "file=@$file;type=application/octet-stream" \
-o "$fileName.zip"
fi

end_ts=$(date +%s)
echo "Upload+response time: $((end_ts - start_ts)) seconds"

echo "Response file size:"
ls -lh "$fileName.zip"

echo "Response file type:"
file "$fileName.zip"

echo "Response file head:"
head -c 200 "$fileName.zip" | cat
echo

unzip -o "$fileName.zip" -d $outputDirectory
rm "$fileName.zip"
Comment on lines +135 to +136
done
fi

echo "Processing $file"
echo "FileName $fileName"
relPath=$relativeFilePath/"$fileName"

echo "Relative file path $relPath"

if [ "$isoauth" = false ]; then
response=$(curl --ntlm -u $alias:$password -H "x-TDBuildWrapper: FluentUI-Android" -X put https://build.intlservices.microsoft.com/api/teams/$id/LocalizableFiles/ParserId/$parserId --form 'FilePath={"FilePath":"'$relPath'"};type=application/json' --form "file=@$file;type=application/octet-stream" -o "$fileName.zip")
echo "Response result LocalizableFiles call $response"
else
tokenValue=$(oauthToken)
response=$(curl -H "Authorization: Bearer $tokenValue" -H "Accept: application/json" -H "x-TDBuildWrapper: FluentUI-Android" -X put https://build.intlservices.microsoft.com/api/teams/$id/LocalizableFiles/ParserId/$parserId --form 'FilePath={"FilePath":"'$relPath'"};type=application/json' --form "file=@$file;type=application/octet-stream" -o "$fileName.zip")
echo "Response result LocalizableFiles call $response"
fi

if [ -f $fileName.zip ]; then
unzip -o $fileName.zip -d $outputDirectory
rm $fileName.zip
fi

done

elif [ -f $filePath ]; then

if [ "$isoauth" = false ]; then
response=$(curl --ntlm -u $alias:$password -H "x-TDBuildWrapper: FluentUI-Android" -X put https://build.intlservices.microsoft.com/api/teams/$id/LocalizableFiles/ParserId/$parserId --form 'FilePath={"FilePath":"'$relativeFilePath'"};type=application/json' --form "file=@$filePath;type=application/octet-stream" -o loc.zip)
echo "Response result for LocalizableFiles call $response"
else
tokenValue=$(oauthToken)
response=$(curl -H "Authorization: Bearer $tokenValue" -H "Accept: application/json" -H "x-TDBuildWrapper: FluentUI-Android" -X put https://build.intlservices.microsoft.com/api/teams/$id/LocalizableFiles/ParserId/$parserId --form 'FilePath={"FilePath":"'$relativeFilePath'"};type=application/json' --form "file=@$filePath;type=application/octet-stream" -o loc.zip)
echo "Response result for LocalizableFiles call $response"
fi

if [ -f loc.zip ]; then
unzip -o loc.zip -d $outputDirectory
rm loc.zip
fi

else

echo "$filePath is not valid"
exit 1

fi


# =========================
# Rename logic (UNCHANGED)
# =========================
if [ "$renameLanguageFolder" = true ]; then
echo "Renaming language folders in $outputDirectory"
languageFolders=$outputDirectory/*/
valuesDir=${outputDirectory%/}
resDir=${valuesDir%/*}

for folder in $languageFolders
do
echo "Folder is: $folder"
folderWithoutTrailingSlash=${folder#${outputDirectory}/}
echo "folderWithoutTrailingSlash is: $folderWithoutTrailingSlash"

if [ $(echo $folderWithoutTrailingSlash | grep -o "-" | wc -l) -gt "1" ]; then
folderWithoutTrailingSlash=b+$(echo "$folderWithoutTrailingSlash" | tr - +)
else
folderWithoutTrailingSlash=$(echo "${folderWithoutTrailingSlash//-/"-r"}")
echo "Renaming language folders in $outputDirectory"
languageFolders=$outputDirectory/*/
valuesDir=${outputDirectory%/}
resDir=${valuesDir%/*}

for folder in $languageFolders
do
folderWithoutTrailingSlash=${folder#${outputDirectory}/}

if [ $(echo $folderWithoutTrailingSlash | grep -o "-" | wc -l) -gt "1" ]; then
folderWithoutTrailingSlash=b+$(echo "$folderWithoutTrailingSlash" | tr - +)
else
folderWithoutTrailingSlash=$(echo "${folderWithoutTrailingSlash//-/"-r"}")
fi

echo "Renaming $folder -> values-$folderWithoutTrailingSlash"
rm -rf "$resDir/values-$folderWithoutTrailingSlash"
mv "$folder" "$outputDirectory/values-$folderWithoutTrailingSlash"
mv "$valuesDir/values-$folderWithoutTrailingSlash" "$resDir"
done
fi

echo "Renaming $folder to "${outputDirectory}/values-${folderWithoutTrailingSlash}""

if [ -d "${resDir}/values-${folderWithoutTrailingSlash}" ]
then
echo "Deleting values-$folderWithoutTrailingSlash from ${resDir} as it already exists"
rm -r "${resDir}/values-${folderWithoutTrailingSlash}"
fi

mv $folder "${outputDirectory}/values-${folderWithoutTrailingSlash}"
mv ${valuesDir}/values-${folderWithoutTrailingSlash} $resDir

done
fi
echo "========== GetLocalizedFiles.sh END =========="
Loading
Loading