Issue
While developing my React Native app, I incorporated several third-party libraries to enhance its functionality. Now, I would like to provide users with the ability to view these libraries within the app settings on their phones. This will allow us to acknowledge and give credit to the libraries we have utilized
Here is what I want.
for IOS: going to the phone settings, scrolling down to the My app, there should be a “Acknowledgements” line/tab to view Upon tapping “Acknowledgements”, there should be a new page with a list of all the open source licenses used
Similarly for android.
I do have explored this package iOS-AcknowledgementGenerator but it seems like it only works for iOS. I have also explored react-native-acknowledgements but it doesn't seems to be working. Any other package or best way probably?
Solution
iOS:
You can use this ruby script to generate acknowledgments. Name the script acknowledgementGenerator-iOS.rb and put it in root folder.
Preconditions:
In order to use this 'AcknowledgementGenerator-iOS' you must have the 'CFPropertyList' gem installed. https://rubygems.org/gems/CFPropertyList
Setup: (Default - Customize as you see fit)
- Create a 'licenses' directory
- Put each license into that directory, one per file, with filenames that end .license
- Perform any necessary reformatting on the licenses. (eg. remove extra spaces at the beginning of lines, ensure that there are no line breaks mid-paragraph). There should be a blank line in-between each paragraph
- Edit your settings bundle Root.plist to include a child section called 'Acknowledgements'
Usage:
- In Terminal execute: (modify to suit your needs) ./acknowledgementGenerator-iOS.rb "path/to/Settings.bundle" "path/to/licenses"
Execute Script At Build Time:
If you want this script to run whenever you build your project, you can add a build phase to your target.
- Select your project file
- Select the target application
- Click the 'Build Phases' tab
- Now from the menu select: Editor > Add Build Phase > Add Run Script Build Phase
- Enter something like the folowing script: (modefy to suit your needs)
if gem list CFPropertyList -i; then ruby path/to/acknowledgementGenerator-iOS.rb "path/to/Settings.bundle" "path/to/licenses"
require 'CFPropertyList'
# Quit unless script gets two arguments
unless ARGV.length == 2
abort("Not the right number of arguments.\n" +
"Usage: ruby acknowledgementGenerator-iOS.rb \"path/to/Settings.bundle\" \"path/to/licenses/\"")
end
# Constants
SETTINGS_BUNDLE = ARGV[0]
LICENSE_FILE_DIR_PATH = ARGV[1]
LICENSE_FILE_EXTENSION = ".license"
ACKNOWLEDGEMENT_GENERATOR_LICENSE = '"THE BEER-WARE LICENSE" (Revision 42):' +
'
' +
'<http://www.knage.net> wrote this file. As long as you retain this notice you ' +
'can do whatever you want with this stuff. If we meet some day, and you think ' +
'this stuff is worth it, you can buy me a beer in return Christophe Vallinas Knage.'
class AcknowledgementGenerator
def createAcknowledgement(name, content)
# Create a arbitrary data structure of basic data types
acknowledgement = {
'StringsTable' => 'ThirdPartyLicenses',
'PreferenceSpecifiers' => [{
'Type' => 'PSGroupSpecifier',
'FooterText' => content
}]
}
# Generate name for acknowledgement plist
acknowledgementName = "Acknowledgement-" + name
# Generate item for acknowledgement list
acknowledgementListItem = {
'Type' => 'PSChildPaneSpecifier',
'Title' => name,
'File' => acknowledgementName
}
# Create plist file
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(acknowledgement)
plist.save(SETTINGS_BUNDLE + "/" + acknowledgementName + ".plist", CFPropertyList::List::FORMAT_XML)
# return
acknowledgementListItem
end
def createAcknowledgementList(acknowledgementListItems)
# Create a arbitrary data structure of basic data types
acknowledgementList = {
'StringsTable' => 'ThirdPartyLicenses',
'PreferenceSpecifiers' => acknowledgementListItems
}
# Create plist file
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(acknowledgementList)
plist.save(SETTINGS_BUNDLE + "/Acknowledgements.plist", CFPropertyList::List::FORMAT_XML)
end
end
if __FILE__ == $PROGRAM_NAME
acknowledgementGenerator = AcknowledgementGenerator.new
# Iterate all license files and generate coresponding acknowledgement plists
acknowledgementListItems = Array.new
Dir.glob(LICENSE_FILE_DIR_PATH + '/*' + LICENSE_FILE_EXTENSION) do |file|
print "Parsing: #{file.split('/').last}..."
# Extract name
licenseFileName = File.basename(file)
projectName = licenseFileName.split('.').first
# Extract license
licenseFile = File.open(file)
projectLicense = licenseFile.read
# Create Acknowledgement- plist
acknowledgementListItem = acknowledgementGenerator.createAcknowledgement(projectName, projectLicense)
licenseFile.close
acknowledgementListItems.push(acknowledgementListItem)
puts " DONE"
end
# Sort the List
sortedAcknowledgementListItems = acknowledgementListItems.sort_by { |item| item["Title"].downcase }
# Create Acknowledgements plist
print "Adding acknowledgements to " + SETTINGS_BUNDLE + "..."
acknowledgementGenerator.createAcknowledgementList(sortedAcknowledgementListItems)
puts " DONE"
# All finished
puts ""
puts "Acknowledgement Generator Finished."
end
Android:
The only option I can find is to show the acknowledgments with in the app. You can use this ruby script to generate acknowledgments. Name the script acknowledgementGenerator-android.rb and put it in root folder.
#!/usr/bin/env ruby
# Quit unless script gets two arguments
unless ARGV.length == 1
abort("Not the right number of arguments.\n" +
"Usage: ruby acknowledgementGenerator-android.rb \"path/to/licenses/\"")
end
# Constants
LICENSE_FILE_DIR_PATH = ARGV[0]
LICENSE_FILE_EXTENSION = ".license"
class AcknowledgementGenerator
def createAcknowledgement(name, content)
# Generate name for acknowledgement
acknowledgementName = "<h3>" + name + "</h3>"
# Generate item for acknowledgement list
htlmlContent = "<pre>" + content + "</pre>"
acknowledgementListItem = {
'liscense' => htlmlContent,
'fileName' => acknowledgementName
}
# return
acknowledgementListItem
end
end
if __FILE__ == $PROGRAM_NAME
acknowledgementGenerator = AcknowledgementGenerator.new
# Iterate all license files and generate coresponding acknowledgement plists
acknowledgementListItems = Array.new
Dir.glob(LICENSE_FILE_DIR_PATH + '/*' + LICENSE_FILE_EXTENSION) do |file|
# Extract name
licenseFileName = File.basename(file)
projectName = licenseFileName.split('.').first
# Extract license
licenseFile = File.open(file)
projectLicense = licenseFile.read
# Create Acknowledgement- plist
acknowledgementListItem = acknowledgementGenerator.createAcknowledgement(projectName, projectLicense)
licenseFile.close
acknowledgementListItems.push(acknowledgementListItem)
end
# Sort the List
sortedAcknowledgementListItems = acknowledgementListItems.sort_by { |item| item["fileName"].downcase }
html = "export const AcknowledgmentsHTML = `
<html>
<head>
<style>
body {
font-family: sans-serif;
}
pre {
background-color: #eeeeee;
padding: 1em;
white-space: pre-wrap;
}
</style>
</head>
<body>"
sortedAcknowledgementListItems.each do |item|
html += item["fileName"]
html += item["liscense"]
end
html += "</body>
</html>
`;
"
File.open(LICENSE_FILE_DIR_PATH + "/acknowledgmentsHTML.js", 'w+') {|f| f.write(html) }
# All finished
# puts ""
puts "Adroid Acknowledgement Generator Finished."
end
Generate Acknowledgements on Android
Run following command in project root directory
./acknowledgementGenerator-android.rb "./licenses"
Generate Acknowledgements on iOS
Run following command in project root directory
./acknowledgementGenerator-iOS.rb "./ios/Settings.bundle" "./licenses"
Answered By - Mohsin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.