Skip to content

Commit

Permalink
A hair pulling endeavor to create a deb file (#14)
Browse files Browse the repository at this point in the history
* Add solid color

* All kbs use config file

* Add version machine

* Add debian control

* Define release blocker

* test this

* Test this

* Define versioned release build

* Build the deb

* Ensure newline at end of file

* Try packaging directly

* Reset after build

* Tweak the service a bit

* Update clean and put json file in right place

* Fix last commit

* Fix last again

* Make the default monitor

* Specify correct architecture

* Changes to control file should rebuild

* Add dependencies

* Try multiple depends lines

* Try comma values

* Add rules file

* Add shlibs depends

* Try doing it this way

* Get it to build

* Clean up a bit

* Make install command

* Output to correct dirs

* This will run as part of the build anyway

* Put it in bin?

* Address some linter errors

* Make things a bit more maintainable

* Stop fucking with my executable dammit

* Clean up the makefile a bit

* Better root access detection

* Fix minor issues with the deb

* Store deb as github artifact

* Install some deps

* Use sudo !

* Use correct deps

* Add a couple more deps

* I should give up spelling and just mash the keyboard

* Put license in the right place?

* Remove unused code from installer

* Also store exe as artifact

* Remove unused keyboard interface

* Reformat

* Clean up rgb a bit

* Clean up monitor keyboard

* Complete cleanup

* We need to not load colors for solid

* Add washed out tests

* Handle negative hsbs

* Cover hex

* Wait for it

* Fix badge

* Make monitor tests

* Rename file
  • Loading branch information
withinboredom authored Nov 28, 2019
1 parent 4dcffd4 commit d073690
Show file tree
Hide file tree
Showing 28 changed files with 569 additions and 163 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,29 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./csharp/coverage.info
deb:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.0.101
- name: apt update
run: sudo apt-get update
- name: install deps
run: sudo apt-get install -y build-essential devscripts lintian debhelper dh-systemd
- name: Build Unsigned Deb
run: make package.deb
- name: Save deb as artifact
uses: actions/upload-artifact@v1
with:
name: package.deb
path: package.deb
- name: Save exe as artifact
uses: actions/upload-artifact@v1
with:
name: keyboard-color
path: release
43 changes: 41 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
.PHONY: clean

SERVICE = keyboard-color
BIN = ${DESTDIR}/usr/bin/keyboard-color
UNIT = ${DESTDIR}/lib/systemd/system/keyboard-colors.service
CONFIG = ${DESTDIR}/etc/keyboard-color.json
COPYRIGHT = ${DESTDIR}/usr/share/doc/s76-keyboard-colors/copyright

${SERVICE}: csharp/keyboards/*.cs csharp/keyboards/*/*.cs
release: csharp/keyboards/*.cs csharp/keyboards/*/*.cs csharp/keyboards/keyboards.csproj version
./version
${MAKE} ${SERVICE}
mv ${SERVICE} release

${SERVICE}: csharp/keyboards/*.cs csharp/keyboards/*/*.cs csharp/keyboards/keyboards.csproj
cd csharp/keyboards && dotnet publish -r linux-x64 -c Release -o ${SERVICE}
mv csharp/keyboards/${SERVICE}/${SERVICE} ${SERVICE}

${BIN}: release
mkdir -p ${shell dirname ${BIN}}
cp release ${BIN}

${UNIT}: keyboard-colors.service
mkdir -p ${shell dirname ${UNIT}}
cp keyboard-colors.service ${UNIT}

${CONFIG}: csharp/keyboards/settings.release.json
mkdir -p ${shell dirname ${CONFIG}}
cp csharp/keyboards/settings.release.json ${CONFIG}

${COPYRIGHT}: LICENSE
mkdir -p ${shell dirname ${COPYRIGHT}}
cp LICENSE ${COPYRIGHT}

../s76-keyboard-colors_1.0_amd64.deb: csharp/keyboards/*.cs csharp/keyboards/*/*.cs csharp/keyboards/keyboards.csproj debian/* csharp/keyboards/settings.release.json keyboard-colors.service
debuild -b -us -uc

package.deb: ../s76-keyboard-colors_1.0_amd64.deb
cp ../s76-keyboard-colors_1.0_amd64.deb package.deb

clean:
cd csharp/keyboards && dotnet clean
rm -rf ${SERVICE}
cd csharp/version && dotnet clean
cd csharp/UnitTests && dotnet clean
rm -rf ${SERVICE} release version package.deb

version: csharp/version/*.cs csharp/version/version.csproj
cd csharp/version && dotnet publish -r linux-x64 -c Release -o version
mv csharp/version/version/version version

install: debian/control debian/changelog debian/rules ${COPYRIGHT} ${CONFIG} ${UNIT} ${BIN}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# System 76 Keyboard Color Shifter

[![Coverage Status](https://coveralls.io/repos/github/withinboredom/system-76-keyboards/badge.svg?branch=fix/codecov)](https://coveralls.io/github/withinboredom/system-76-keyboards?branch=fix/codecov)
[![Coverage Status](https://coveralls.io/repos/github/withinboredom/system-76-keyboards/badge.svg?branch=master)](https://coveralls.io/github/withinboredom/system-76-keyboards?branch=master)

![Build Status](https://github.com/withinboredom/system-76-keyboards/workflows/Builder/badge.svg)

Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.1.0-beta.5
10 changes: 9 additions & 1 deletion csharp/UnitTests/ColorSpace/RgbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ public void CanConvertHsb()
Assert.AreEqual(expected, new Rgb(color));
}

[Test]
public void CanHandleNegativeHsb()
{
var color = new Hsb(0, -10, -10);
var expected = Rgb.Empty;
Assert.AreEqual(expected, new Rgb(color));
}

[Test]
public void CanConvertHex()
{
var color = Rgb.FromHex("FF00FF");
var color = Rgb.FromHex("#FF00FF");
Assert.AreEqual(new Rgb(255, 0, 255), color);
Assert.AreEqual("FF00FF", color.Hex);
}
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions csharp/UnitTests/Filters/WashedOutTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading.Tasks;
using keyboards.ColorSpace;
using keyboards.Filters;
using NUnit.Framework;

namespace UnitTests.Filters
{
public class WashedOutTests
{
[Test]
public async Task IncreasesBrightness()
{
var color = Hsb.Empty;
var filter = new WashedOut();
var returned = await filter.ApplyFilter(new Rgb(color));
Assert.AreEqual(color.SetBrightness(1), new Hsb(returned));
}
}
}
15 changes: 15 additions & 0 deletions csharp/UnitTests/Keyboards/MonitorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using keyboards.Keyboards;
using NUnit.Framework;

namespace UnitTests.Keyboards
{
public class MonitorTests
{
[Test]
public void CanBeCreated()
{
var monitor = new Monitor(new FakeContainer(), null);
Assert.IsTrue(monitor.Filters.Length == 0);
}
}
}
1 change: 1 addition & 0 deletions csharp/UnitTests/Sides/SolidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void ResetFixture()
public void TestSolidColor()
{
var side = new Solid(Rgb.FromHex("FFFFFF")) {Led = _fixtureFile};
side.Load().Wait();
side.Commit(new IFilter[] { });
Assert.AreEqual("FFFFFF", side.Led.Contents);
}
Expand Down
25 changes: 25 additions & 0 deletions csharp/csharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "keyboards", "keyboards\keyb
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{6E7A6890-8B4F-435A-AF24-2C9698425097}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "repository", "repository", "{C8E1484E-48BC-447D-B78E-79E6B4B6BA47}"
ProjectSection(SolutionItems) = preProject
..\README.md = ..\README.md
..\Makefile = ..\Makefile
..\keyboard-colors.service = ..\keyboard-colors.service
..\VERSION = ..\VERSION
..\.github\workflows\tests.yml = ..\.github\workflows\tests.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "version", "version\version.csproj", "{47D800E0-CE45-4A54-A05D-D3A1B6B3D3A9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "debian", "debian", "{5C7934AE-7958-4871-B8EB-950824ABEDDA}"
ProjectSection(SolutionItems) = preProject
..\debian\changelog = ..\debian\changelog
..\debian\control = ..\debian\control
..\debian\rules = ..\debian\rules
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +36,12 @@ Global
{6E7A6890-8B4F-435A-AF24-2C9698425097}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E7A6890-8B4F-435A-AF24-2C9698425097}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E7A6890-8B4F-435A-AF24-2C9698425097}.Release|Any CPU.Build.0 = Release|Any CPU
{47D800E0-CE45-4A54-A05D-D3A1B6B3D3A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47D800E0-CE45-4A54-A05D-D3A1B6B3D3A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47D800E0-CE45-4A54-A05D-D3A1B6B3D3A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47D800E0-CE45-4A54-A05D-D3A1B6B3D3A9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5C7934AE-7958-4871-B8EB-950824ABEDDA} = {C8E1484E-48BC-447D-B78E-79E6B4B6BA47}
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion csharp/keyboards/ColorSpace/RGB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Rgb(Rgb rgb)
/// <param name="other">The color to convert</param>
public Rgb(Hsb other)
{
double r = 0, b = 0, g = 0;
double r, b, g;

if (other.Brightness < 0)
{
Expand Down Expand Up @@ -168,6 +168,7 @@ public override string ToString()
/// <returns></returns>
public static Rgb FromHex(string hex)
{
if (hex.StartsWith("#")) hex = hex.Substring(1);
var red = HexToByte(hex.Substring(0, 2));
var gre = HexToByte(hex.Substring(2, 2));
var blu = HexToByte(hex.Substring(4, 2));
Expand Down
75 changes: 10 additions & 65 deletions csharp/keyboards/Installer.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,25 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

namespace keyboards
{
public static class Installer
{
private static string? Parameters { get; set; }
[DllImport("libX11")]
private static extern IntPtr XOpenDisplay(string displayName);

private static string SystemD =>
$@"
[Unit]
Description=System76 Keyboard Colors
[Service]
Type=Simple
ExecStart=/usr/local/bin/keyboard-color {Parameters}
PIDFile=keyboard-colors.pid
[Install]
WantedBy=multi-user.target
";

internal static void CreateParametersFromOptions(string[] options)
{
Parameters = string.Join(' ', options.Where(s => !s.Contains("--install") || !s.Contains("-i")).ToArray());
}

private static void PutMeInRightSpot()
{
var path = Environment.CurrentDirectory + "/keyboard-color";

if (!File.Exists(path) && File.Exists("/usr/local/bin/keyboard-color"))
return;

if (!File.Exists(path))
{
Console.WriteLine("Unable to locate `keyboard-color` in the current directory.");
Environment.Exit(1);
}

Console.WriteLine($"Copying {path} to /usr/local/bin");
File.Copy(path, "/usr/local/bin/keyboard-color", true);

if (File.Exists("/opt/keyboard-colors/keyboard-color.php"))
Console.WriteLine("Please delete /opt/keyboard-colors/keyboard-color.php as it's no longer needed.");
}
[DllImport("libX11")]
private static extern void XCloseDisplay(IntPtr display);

internal static bool RootHasPermission()
{
var process = Process.Start(new ProcessStartInfo("xhost") {RedirectStandardOutput = true});

if (process == null) return false;
var hasAccess = true;
var display = XOpenDisplay(":0");
if (display == IntPtr.Zero) hasAccess = false;

if (!process.WaitForExit((int) TimeSpan.FromSeconds(10).TotalMilliseconds))
return false;
XCloseDisplay(display);

var result = process.StandardOutput.ReadToEnd();
return result.Contains("SI:localuser:root");
return hasAccess;
}

private static bool IsInProfile()
Expand All @@ -67,26 +30,8 @@ private static bool IsInProfile()
return contents.Contains("SI:localuser:root");
}

private static void CreateService()
{
var servicePath = "/etc/systemd/system/keyboard-colors.service";
File.WriteAllText(servicePath, SystemD);
}

internal static void Install()
{
Console.WriteLine("Copying `keyboard-color` to /usr/local/bin");
PutMeInRightSpot();

Console.WriteLine("Creating service file in /etc/systemd/system/keyboard-colors.service");
CreateService();

Console.WriteLine("If you want to start this on boot run:");
Console.WriteLine(" sudo systemctl enable keyboard-colors");

Console.WriteLine("If you want to start the service right now, run:");
Console.WriteLine(" sudo systemctl start keyboard-colors\n");

if (!RootHasPermission())
{
Console.WriteLine(
Expand Down
Loading

0 comments on commit d073690

Please sign in to comment.