feat: add support for Flutter Linux, macOS and Windows (#10)

This commit is contained in:
Aleksey Kulikov 2021-10-30 13:54:25 +03:00 committed by GitHub
parent a71bb14b86
commit c8895524be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 401 additions and 18 deletions

View file

@ -17,18 +17,19 @@ jobs:
fail-fast: false fail-fast: false
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1 - uses: subosito/flutter-action@v1
with: with:
sdk: dev channel: dev
flutter-version: "2.6.0-11.0.pre"
- name: Install dependencies - name: Install dependencies
run: dart pub get run: flutter pub get
- name: Verify formatting - name: Verify formatting
run: dart format --output=none --set-exit-if-changed . run: flutter format --output=none --set-exit-if-changed .
- name: Analyze source code - name: Analyze source code
run: dart analyze --fatal-infos run: flutter analyze --fatal-infos
test: test:
needs: analyze needs: analyze
@ -45,15 +46,16 @@ jobs:
git config --global core.eol lf git config --global core.eol lf
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1 - uses: subosito/flutter-action@v1
with: with:
sdk: dev channel: dev
flutter-version: "2.6.0-11.0.pre"
- name: Install dependencies - name: Install dependencies
run: dart pub get run: flutter pub get
- name: Download libgit2 library - name: Download libgit2 library
run: dart run libgit2dart:setup run: flutter pub run libgit2dart:setup
- name: Run tests - name: Run tests
run: dart test --exclude-tags "remote_fetch" --platform vm run: dart test --exclude-tags "remote_fetch" --platform=vm

49
.gitignore vendored
View file

@ -1,11 +1,50 @@
.vscode/ .vscode/
.packages
.dart_tool
.pub
pubspec.lock
# Coverage # Coverage
coverage/ coverage/
# libgit2 headers for ffigen # libgit2 headers for ffigen
libgit2/ libgit2/
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
pubspec.lock
/build/
# Web related
lib/generated_plugin_registrant.dart
# Symbolication related
app.*.symbols
# Obfuscation related
app.*.map.json
# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release

10
.metadata Normal file
View file

@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 4b330ddbedab445481cc73d50a4695b9154b4e4f
channel: dev
project_type: plugin

View file

@ -36,9 +36,13 @@ Future<void> download(String platform) async {
logger.stdout('${ansi.green}libgit2 for $platform is already available.'); logger.stdout('${ansi.green}libgit2 for $platform is already available.');
} else { } else {
logger.stdout( logger.stdout(
'${ansi.red}libgit2 for $platform is outdated. Run: \n' '${ansi.red}libgit2 for $platform is outdated.\n'
'If it is dart application run: \n'
'dart run libgit2dart:setup clean\n'
'dart run libgit2dart:setup\n\n'
'If it is flutter application run: \n'
'flutter pub run libgit2dart:setup clean\n' 'flutter pub run libgit2dart:setup clean\n'
'flutter pub run libgit2dart:setup', 'flutter pub run libgit2dart:setup\n\n',
); );
} }
} else { } else {

View file

@ -67,7 +67,15 @@ DynamicLibrary loadLibrary(String name) {
'To download the library, please run the following command from the ' 'To download the library, please run the following command from the '
'root of your project:', 'root of your project:',
); );
logger.stdout('${ansi.yellow}dart run libgit2dart:setup${ansi.none}'); logger.stdout(
'${ansi.yellow}dart run libgit2dart:setup${ansi.none} for '
'dart application',
);
logger.stdout(ansi.none);
logger.stdout(
'${ansi.yellow}flutter pub run libgit2dart:setup${ansi.none} for '
'flutter application',
);
logger.stdout(ansi.none); logger.stdout(ansi.none);
rethrow; rethrow;
} }

25
linux/CMakeLists.txt Normal file
View file

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10)
set(PROJECT_NAME "libgit2dart")
project(${PROJECT_NAME} LANGUAGES CXX)
# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "libgit2dart_plugin")
add_library(${PLUGIN_NAME} SHARED
"libgit2dart_plugin.cc"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK)
# List of absolute paths to libraries that should be bundled with the plugin
set(libgit2dart_bundled_libraries
"${CMAKE_CURRENT_SOURCE_DIR}/libgit2-1.3.0.so"
PARENT_SCOPE
)

View file

@ -0,0 +1,26 @@
#ifndef FLUTTER_PLUGIN_LIBGIT2DART_PLUGIN_H_
#define FLUTTER_PLUGIN_LIBGIT2DART_PLUGIN_H_
#include <flutter_linux/flutter_linux.h>
G_BEGIN_DECLS
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
#else
#define FLUTTER_PLUGIN_EXPORT
#endif
typedef struct _Libgit2dartPlugin Libgit2dartPlugin;
typedef struct {
GObjectClass parent_class;
} Libgit2dartPluginClass;
FLUTTER_PLUGIN_EXPORT GType libgit2dart_plugin_get_type();
FLUTTER_PLUGIN_EXPORT void libgit2dart_plugin_register_with_registrar(
FlPluginRegistrar* registrar);
G_END_DECLS
#endif // FLUTTER_PLUGIN_LIBGIT2DART_PLUGIN_H_

BIN
linux/libgit2-1.3.0.so Normal file

Binary file not shown.

View file

@ -0,0 +1,70 @@
#include "include/libgit2dart/libgit2dart_plugin.h"
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
#include <cstring>
#define LIBGIT2DART_PLUGIN(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), libgit2dart_plugin_get_type(), \
Libgit2dartPlugin))
struct _Libgit2dartPlugin {
GObject parent_instance;
};
G_DEFINE_TYPE(Libgit2dartPlugin, libgit2dart_plugin, g_object_get_type())
// Called when a method call is received from Flutter.
static void libgit2dart_plugin_handle_method_call(
Libgit2dartPlugin* self,
FlMethodCall* method_call) {
g_autoptr(FlMethodResponse) response = nullptr;
const gchar* method = fl_method_call_get_name(method_call);
if (strcmp(method, "getPlatformVersion") == 0) {
struct utsname uname_data = {};
uname(&uname_data);
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
g_autoptr(FlValue) result = fl_value_new_string(version);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
fl_method_call_respond(method_call, response, nullptr);
}
static void libgit2dart_plugin_dispose(GObject* object) {
G_OBJECT_CLASS(libgit2dart_plugin_parent_class)->dispose(object);
}
static void libgit2dart_plugin_class_init(Libgit2dartPluginClass* klass) {
G_OBJECT_CLASS(klass)->dispose = libgit2dart_plugin_dispose;
}
static void libgit2dart_plugin_init(Libgit2dartPlugin* self) {}
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
gpointer user_data) {
Libgit2dartPlugin* plugin = LIBGIT2DART_PLUGIN(user_data);
libgit2dart_plugin_handle_method_call(plugin, method_call);
}
void libgit2dart_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
Libgit2dartPlugin* plugin = LIBGIT2DART_PLUGIN(
g_object_new(libgit2dart_plugin_get_type(), nullptr));
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_autoptr(FlMethodChannel) channel =
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
"libgit2dart",
FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(channel, method_call_cb,
g_object_ref(plugin),
g_object_unref);
g_object_unref(plugin);
}

View file

@ -0,0 +1,19 @@
import Cocoa
import FlutterMacOS
public class Libgit2dartPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "libgit2dart", binaryMessenger: registrar.messenger)
let instance = Libgit2dartPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getPlatformVersion":
result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
default:
result(FlutterMethodNotImplemented)
}
}
}

BIN
macos/libgit2-1.3.0.dylib Normal file

Binary file not shown.

23
macos/libgit2dart.podspec Normal file
View file

@ -0,0 +1,23 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint libgit2dart.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'libgit2dart'
s.version = '0.0.1'
s.summary = 'Dart bindings to libgit2.'
s.description = <<-DESC
Dart bindings to libgit2.
DESC
s.homepage = 'https://github.com/SkinnyMind/libgit2dart'
s.license = { :file => '../LICENSE' }
s.author = { 'Aleksey Kulikov' => 'skinny.mind@gmail.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'FlutterMacOS'
s.vendored_libraries = 'libgit2-1.3.0.dylib'
s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'
end

View file

@ -4,6 +4,7 @@ version: 0.0.1
environment: environment:
sdk: ">=2.15.0-82.0.dev <3.0.0" sdk: ">=2.15.0-82.0.dev <3.0.0"
flutter: ">=2.5.0"
dependencies: dependencies:
archive: ^3.1.6 archive: ^3.1.6
@ -17,6 +18,16 @@ dev_dependencies:
path: ^1.8.0 path: ^1.8.0
test: ^1.17.5 test: ^1.17.5
flutter:
plugin:
platforms:
linux:
pluginClass: Libgit2dartPlugin
macos:
pluginClass: Libgit2dartPlugin
windows:
pluginClass: Libgit2dartPlugin
ffigen: ffigen:
output: "lib/src/bindings/libgit2_bindings.dart" output: "lib/src/bindings/libgit2_bindings.dart"
headers: headers:

17
windows/.gitignore vendored Normal file
View file

@ -0,0 +1,17 @@
flutter/
# Visual Studio user-specific files.
*.suo
*.user
*.userosscache
*.sln.docstates
# Visual Studio build-related files.
x64/
x86/
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

24
windows/CMakeLists.txt Normal file
View file

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.14)
set(PROJECT_NAME "libgit2dart")
project(${PROJECT_NAME} LANGUAGES CXX)
# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "libgit2dart_plugin")
add_library(${PLUGIN_NAME} SHARED
"libgit2dart_plugin.cpp"
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
# List of absolute paths to libraries that should be bundled with the plugin
set(libgit2dart_bundled_libraries
"${CMAKE_CURRENT_SOURCE_DIR}/libgit2-1.3.0.dll"
PARENT_SCOPE
)

View file

@ -0,0 +1,23 @@
#ifndef FLUTTER_PLUGIN_LIBGIT2DART_PLUGIN_H_
#define FLUTTER_PLUGIN_LIBGIT2DART_PLUGIN_H_
#include <flutter_plugin_registrar.h>
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport)
#else
#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport)
#endif
#if defined(__cplusplus)
extern "C" {
#endif
FLUTTER_PLUGIN_EXPORT void Libgit2dartPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar);
#if defined(__cplusplus)
} // extern "C"
#endif
#endif // FLUTTER_PLUGIN_LIBGIT2DART_PLUGIN_H_

BIN
windows/libgit2-1.3.0.dll Normal file

Binary file not shown.

View file

@ -0,0 +1,82 @@
#include "include/libgit2dart/libgit2dart_plugin.h"
// This must be included before many other Windows headers.
#include <windows.h>
// For getPlatformVersion; remove unless needed for your plugin implementation.
#include <VersionHelpers.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <map>
#include <memory>
#include <sstream>
namespace {
class Libgit2dartPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
Libgit2dartPlugin();
virtual ~Libgit2dartPlugin();
private:
// Called when a method is called on this plugin's channel from Dart.
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
};
// static
void Libgit2dartPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarWindows *registrar) {
auto channel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
registrar->messenger(), "libgit2dart",
&flutter::StandardMethodCodec::GetInstance());
auto plugin = std::make_unique<Libgit2dartPlugin>();
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto &call, auto result) {
plugin_pointer->HandleMethodCall(call, std::move(result));
});
registrar->AddPlugin(std::move(plugin));
}
Libgit2dartPlugin::Libgit2dartPlugin() {}
Libgit2dartPlugin::~Libgit2dartPlugin() {}
void Libgit2dartPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("getPlatformVersion") == 0) {
std::ostringstream version_stream;
version_stream << "Windows ";
if (IsWindows10OrGreater()) {
version_stream << "10+";
} else if (IsWindows8OrGreater()) {
version_stream << "8";
} else if (IsWindows7OrGreater()) {
version_stream << "7";
}
result->Success(flutter::EncodableValue(version_stream.str()));
} else {
result->NotImplemented();
}
}
} // namespace
void Libgit2dartPluginRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar) {
Libgit2dartPlugin::RegisterWithRegistrar(
flutter::PluginRegistrarManager::GetInstance()
->GetRegistrar<flutter::PluginRegistrarWindows>(registrar));
}